UbuntuThis forum is for the discussion of Ubuntu Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
It is suppose to test that the current day of the month is <= 7th and do the `echo`, on monday.
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"
(or /bin/date) give the error: "/bin/sh: 1: Syntax error: EOF in backquote substitution"
Note that I am using this cron on Ubuntu 15.10. The crontab entry as initially shown works fine on Slackware.
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:
&&
between it and the action.
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.
The use of backtick `` is deprecated, $() is the preferred methodology
If you'll notice in my original example I did use $(). Given the error 'Syntax error: end of file unexpected (expecting ")")', I thought the parenthesis might be an issue and I changed to `` to test that.
Quote:
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.
In reality, I am using a script. My "echo yup" is a test stand-in. The test is simply determining when the script should run which, I think, is the job of a crontab entry.
Quote:
Originally Posted by wpeckham
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:
&&
between it and the action.
Well, that's essentially what I'm doing with the &&. As I said in response to TenTenths, my "echo yup" is a test stand-in for the actual script. I have always commonly done the when-to-run logic in the crontab itself, not the called script -- and I want the same NOW!
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.
Hmmm, on this Ubuntu system /bin/sh is slinked /bin/dash -- whatever that is. However, your point is worth some experimentation.
Quote:
2. Try using
Quote:
if [ .. ] ; then ... fi
instead of
Quote:
test
and see if that works better.
Perhaps you didn't read the WHOLE posting. That's exactly what I show in my 2nd example where I say "Changing to:".
Quote:
Originally Posted by rknichols
The "%" sign is special in a crontab and needs to be escaped with a backslash to be treated as a regular character.
Interesting that the Slackware cron does not require % to be escaped.
Slackware uses "Dillon's lightweight cron daemon", which does not support some of the features of other cron daemon implementations, such as the use of the "%" sign to provide data on stdin to the command. Quote from `man 5 crontab`:
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 +
Unsurprisingly, it didn't like that truncated command line.
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
NOTE do not just use '*' in the first two columns That means cron will run it EVERY MINUTE on the days selected
The above will only attempt to run 7 times every month.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.