0

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

mk_89
  • 125
  • 1
  • 2
  • 8

1 Answers1

0

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:

NOW=$(date)
* * * * * 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.

HBruijn
  • 77,029
  • 24
  • 135
  • 201
  • 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