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
There are four common causes for cron job commands to behave differently compared to commands typed directly into an interactive shell:
$PATH
, and other expected variables missing./bin/sh
by default, whereas you may be using some other shell interactively.%
character specially (it is turned into a newline in the command).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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With