[ACCEPTED]-Cron fails on single apostrophe-cron

Accepted answer
Score: 30

There are four common causes for cron job 11 commands to behave differently compared 10 to commands typed directly into an interactive 9 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 8 a \ in a crontab file, which tells cron to 7 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 6 matter, always put double quotes around 5 a "$variable_substitution" or a "$(command substitution)", unless you know why not do it 4 in a particular case. Otherwise, if the 3 variable contents or command output contains 2 whitespace or ?*\[, they will be interpreted 1 by the shell.)

Score: 0

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

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

should 1 work.

More Related questions