15

The following does work as expected:

date +'%d-%b-%Y-%H-%M'

28-Sep-2009-14-28

But none of the following 4 entries from crontab are working.

* * * * * date +\'%d-%b-%Y-%H-%M\' >> /backup/shantanu/testing.txt
* * * * * date +'%d-%b-%Y-%H-%M' >> /backup/shantanu/testing1.txt
* * * * * date +"%d-%b-%Y-%H-%M" >> /backup/shantanu/testing2.txt
* * * * * date +\"%d-%b-%Y-%H-%M\" >> /backup/shantanu/testing3.txt
Error:
/bin/sh: -c: line 0: unexpected EOF while looking for matching `"'
/bin/sh: -c: line 1: syntax error: unexpected end of file

I can save the same code in a shell script and set the cron, but I will like to know if it is possible to directly set a cron for the task.

The actual cron entry that I am trying to set looks something like this...

16 * * * * mysqldump myDB myTB > /backup/ABCbc$(date +'%d-%b-%Y-%H-%M').sql 2> /backup/ABCbc_errORS$(date +'%d-%b-%Y-%H-%M').txt
Jeremy Stein
  • 19,171
  • 16
  • 68
  • 83
shantanuo
  • 31,689
  • 78
  • 245
  • 403
  • If you resolved your problem, please post it as an answer. – Jeremy Stein Jan 04 '10 at 20:58
  • 58 13 * * * /usr/bin/mysqldump -uroot -pPassWord --compact --databases db1 db2 db3 db4 | zip > /home/develop/backup/central_mydt$(date +'\%d\%m\%y').zip – shantanuo Jan 05 '10 at 14:59
  • Possible duplicate of [How is % special in crontab?](https://stackoverflow.com/questions/5277508/how-is-special-in-crontab) – tripleee Feb 03 '19 at 07:27

3 Answers3

34

There are four common causes for cron job commands to behave differently compared to commands typed directly into an interactive shell:

  • Cron provides a limited environment, e.g., a minimal $PATH, and other expected variables missing.
  • Cron invokes /bin/sh by default, whereas you may be using some other shell interactively.
  • Cron treats the % character specially (it is turned into a newline in the command).
  • The command may behave differently because it doesn't have a terminal available.

You must precede all % characters with a \ in a crontab file, which tells cron to just put a % in the command, e.g.

16 * * * * mysqldump myDB myTB > "/backup/ABCbc$(date +'\%d-\%b-\%Y-\%H-\%M').sql" 2> "/backup/ABCbc_errORS$(date +'\%d-\%b-\%Y-\%H-\%M').txt"

(As a separate matter, always put double quotes around a "$variable_substitution" or a "$(command substitution)", unless you know why not do it in a particular case. Otherwise, if the variable contents or command output contains whitespace or ?*\[, they will be interpreted by the shell.)

Community
  • 1
  • 1
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
0

As long as there are no spaces in the format string supplied as an argument to date, you should not need the ticks at all.

date +%d-%b-%Y-%H-%M

should work.

Tim Post
  • 33,371
  • 15
  • 110
  • 174
  • 1
    I removed the quotes as mentioned above and it still does not work from cron. It runs fine from a command line. – shantanuo Sep 28 '09 at 09:48
0

You're using a syntax not supported by /bin/sh. Try invoking your preferred shell and passing the command as an argument.

Jeremy Stein
  • 19,171
  • 16
  • 68
  • 83