problem using test in cron
This is probably something simple ...
I have the following crontab entry: Code:
* * * * mon /bin/bash -c "test $(date +%d) -le 07 && echo yup >>/root/yup.log 2>&1" The command `test $(date +%d) -le 07 && echo yup` works just fine at a command prompt, but in the crontab I get an error email: /bin/sh: 1: Syntax error: end of file unexpected (expecting ")") I've stared at this for a long time and can't for the life of me see what's wrong. I'm also a bit confused about the "/bin/sh" bit because I'm explicitly specifying /bin/bash. Changing to: Code:
* * * * mon /bin/bash -c "if [ `date +%d` -le 07 ]; then echo yup; fi >>/root/yup.log 2>&1" Note that I am using this cron on Ubuntu 15.10. The crontab entry as initially shown works fine on Slackware. |
Couple of suggestions:
The use of backtick `` is deprecated, $() is the preferred methodology Rather than try to write your script as a one-liner in the cron you may find it's better to just have a bash script that does all your tests etc. That way you can expand it much more easily. |
Personally, if I require conditionals (very common, last day of month, first week of month on wednesday, etc.) I write a small script and CALL it from cron, or call a conditional script and
Quote:
1. Check, and see if /bin/sh really happens to be bash in disguise. If so stop explicitly calling bash and try just the command. Although it is in posix mode, most built-in and extensions work the same. 2. Try using Quote:
Quote:
|
Moderator response
Moved: This thread is more suitable in <Ubuntu> and has been moved accordingly to help your thread/question get the exposure it deserves.
Requested by OP |
The "%" sign is special in a crontab and needs to be escaped with a backslash to be treated as a regular character.
Code:
* * * * mon /bin/bash -c "test $(date +\%d) -le 07 && echo yup >>/root/yup.log 2>&1" |
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
Interesting that the Slackware cron does not require % to be escaped. |
Quote:
The "sixth" field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL vari- able of the cronfile. Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.Also, the reason that the complaint came from "/bin/sh" is that the command line is first passed to the shell specified at the top of the crontab, so what it saw was a command to invoke /bin/bash with some arguments: Code:
/bin/bash -c "test $(date + |
I typically I have cron test the day of month, and than the shell test the day of week.
Also it is run in sh. BUT remember this may not be BASH, so using backtic while depreciated is recommended. Code:
0 0 1-7 * * [ `date '+\%u'` -eq 1 ] && CMD The above will only attempt to run 7 times every month. |
All times are GMT -5. The time now is 08:57 PM. |