Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm basically trying to declare a variable with a date() value in crontab as I will constantly be using the same date structure over and over, here is what my crontab looks like so far.

php     = '/usr/bin/php'
dir     = '/var/www/bkd'
logs    = '/var/www/bkd/logs'

#10 23 * * *   $php    $dir/inventory/delete.php >>     $logs/delete/delete_$(/bin/date +\%Y\%m\%d\%H\%M\%S).txt
31 00 * * 7    $php    $dir/prices/all.php >>           $logs/prices_all/prices_all_$(/bin/date +\%Y\%m\%d\%H\%M\%S).txt
#30 21 * * 3   $php    $dir/ranks/all.php >>            $logs/ranks/ranks_all_$(/bin/date +\%Y\%m\%d\%H\%M\%S).txt

But I would prefer to have something like this... where the time variable is being used at the end

php     = '/usr/bin/php'
dir     = '/var/www/bkd'
logs    = '/var/www/bkd/logs'
time    = $(/bin/date +\%Y\%m\%d\%H\%M\%S).txt

#10 23 * * *   $php    $dir/inventory/delete.php >>     $logs/delete/delete_$time
31 00 * * 7    $php    $dir/prices/all.php >>           $logs/prices_all/prices_all_$time
#30 21 * * 3   $php    $dir/ranks/all.php >>            $logs/ranks/ranks_all_$time

Is there anyway something like this could be possible in crontab? thank you in advanced

share|improve this question

No, it seems that variables in a crontab in my RHEL 6 at least are not parsed as a shell script would be, a simple test scenario with a crontab like this:

* * * * * echo $NOW >> /tmp/logfile

results in a logfile full of literal $(date) lines

I would suggest each script maintaining it's own output files instead of redirecting stdout from cron, but that is as much a personal preference as anything.

share|improve this answer
yeh, you're right I guess, the best and simplest solution I came up with was time = 'date +%Y-%m-%d_%H-%M-%S' #10 23 * * * $php $dir/inventory/delete.php >> $logs/delete/delete_$($time).txt – mk_89 Jul 13 '14 at 10:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.