1

I have a problem using the linux 'date' command within a cronjob.

Linux version: CentOS Linux 7 (Core)

What i need is the date of 9 days ago as a parameter to my test.sh script. Today is 10-08-2016, the job that needs to be executed is:

/scripts/test.sh 2016-08-01

My code in crontab:

DATEVAR=$(date +%F --date="9 days ago")
0 12 * * tue ~/scripts/test.sh $($DATEVAR)

So the linux command is 'date +%F --date="9 days ago"' but i need this to be executed and set as parameter. What it does now is run the script with as parameter '$(date':

~/scripts/test.sh $(date

I have tried setting the DATEVAR with the following things without succes:

DATEVAR='date +%F --date="9 days ago"'   
DATEVAR=date +%F --date="9 days ago"
DATEVAR=$(date +%F --date="9 days ago")
DATEVAR=(shell date +%F --date="9 days ago")

Does anyone know if this is possible and how my DATEVAR can be set with the result of executing the 'date' command?

Anne Derks
  • 13
  • 5
  • There isn't enough information here to provide an authoritative answer. What flavour of Linux are you using? Some (Ubuntu?) don't use bash as their `/bin/sh`, and some shells may not support `$(..)` notation. Try backquotes instead. Also experiment with escaping your percent characters. Also, `$($DATEVAR)` is probably wrong (in your crontab), as you'll be executing the output of the `date` command as if it were a comment itself. – ghoti Aug 10 '16 at 13:49
  • Possible duplicate of http://stackoverflow.com/questions/27123367/percent-sign-not-working-in-crontab – tripleee Aug 10 '16 at 15:34

2 Answers2

0

I am not sure you can do it inside a cron job.

A simple workaround is to wrap your call to test.sh into another script without argument. In it, simply write:

DATEVAR=$(date +%F --date="9 days ago")
~/scripts/test.sh $($DATEVAR)

An call this one in your cronjob.

Derlin
  • 9,572
  • 2
  • 32
  • 53
0

This should work:

0 12 * * tue  _DV=`date +\%F --date="9 days ago"`; ~/scripts/test.sh $_DV

Unfortunately, there are several issues to overcome here. First, the fact that cron now supports "environment settings" can be very misleading. Folks with shell script experience might easily assume that the full power of the shell can be used here -- it cannot. These settings are as dumb as they come: they are strict verbatim replacements. In my mind, a better moniker would be "placeholder assignment."

Second, the date string you wish to utilize is somewhat complex. Specifically, it contains an embedded quoted string ("9 days ago") and a special crontab character (surprise!): the '%' in the expression '+%F'. Cron replaces '%' with newlines -- a nice feature but surprising if you're unaware or forget about it. And quotes don't survive the assignment phase.

Knowing this, an alternative to the above entry that uses "placeholder assignment" is:

DT1 = date +%F
DT2 = 9 days ago

0 12 * * tue  _DV=`$DT1 --date="$DT2"`; ~/scripts/test.sh $_DV

Here we're capturing pieces that we want to be directly substituted without any interpolation.

There -- two solutions for the price of one!

GLRoman
  • 593
  • 7
  • 10