Cannot run bash script from crontab when it works from command line bash
I note in the crontab(5) man page, this:
The ``sixth'' field (the rest of the line) specifies the command to be run. The entire command portion of the line will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile.
So, you may want to specify bash either with
SHELL=/bin/bash
or, as the above affects all cron scripts, this
* * * * 1-5 root bash /home/xxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/jmeter-cron-randomise.sh >> /home/xxxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/cron.log
# ...............^^^^
Related videos on Youtube
kal
Updated on September 18, 2022Comments
-
kal over 1 year
I have a strange problem of being to able to run a bash script from commandline but not from the crontab entry for root. I am running Ubuntu 12.04.
* * * * 1-5 root /home/xxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/jmeter-cron-randomise.sh >> /home/xxxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/cron.log
if i run the script from the cmd line using bash it works fine but fails with sh with following error:
jmeter-cron-randomise.sh: 7: jmeter-cron-randomise.sh: arithmetic expression: expecting primary: " % 1 "
Having googled the problem it seems like stand shell doesn't have the same maths operators like % (modulus) like bash. Not sure why the cron job is failing in the script? i am assumming its because its not using the bash shell? It's definitely being fired by the cron daemon (can see it in /var/log/syslog ). Any help much appreciated.
script causing the problems
#!/bin/bash echo Running the jmeter-cron-randomiser script script="/home/xxxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/jmeter-cron.sh" min=$(( 1 * 1 )) rmin=$(( $RANDOM % $min )) echo "min = ${min}"; echo "rmin = ${rmin}" at -f "$script" now+${rmin}min
-
MGodby over 9 yearsDoes your script begin with a shebang, i.e.
#!/bin/bash
? -
kal over 9 yearsyes it does #!/bin/bash
-
kal over 9 yearsi think the key is the difference between running the sh and the bash script and why there is an arithmetic exception with sh but not with bash from the command line. I think thats where its failing when its fired from cron.
-
MGodby over 9 yearsThis will probably require more information about your script if you want help troubleshooting its behavior.
-
kal over 9 years#!/bin/bash echo Running the jmeter-cron-randomiser script script="/home/xxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/jmeter-cron.sh" min=$(( 1 * 1 )) rmin=$(( $RANDOM % $min )) echo "min = ${min}"; echo "rmin = ${rmin}" at -f "$script" now+${rmin}min '
-
MGodby over 9 yearsPlease update your original question and use appropriate formatting for readability.
-
kal over 9 yearsi am varying the times for testing purposes but it basically something i picked up from net for randomly firing off another script in the next x mins/hours etc
-
MGodby over 9 yearsUpon further inspection, I think that $RANDOM does not exist in your script because cron launched from a non-bash shell like you said.
-
kal over 9 yearsyes thats the crux of the problem. when I run from sh $RANDOM is not resolved but is from bash
-
Volker Siegel over 9 yearsWhat is the exact name of the
crontab
file?/etc/crontab
? -
Alaa Ali over 9 yearsIf you're using root's crontab (that is, if you're editing it using
crontab -e
while logged in as root), you shouldn't put the username in the cronjob command. Remove the wordroot
before the command. You should only put a username when adding cronjobs in/etc/crontab
. And there are a lot of troubleshooting steps you can do here mate. Comment out everything on the script and just put some echo lines that append to a file, likeecho $SHELL > /home/xxxxx/tesfile
just to see if it actually works. Then add in your script lines but remove the$RANDOM
var to see if it works. Troubleshoot.
-
-
kal over 9 yearsalready tried the
SHELL=/bin/bash
in the crontab entry and also and but no luck. -
kal over 9 yearsmeant to say that i tried the
SHELL=/bin/bash in the crontab entry
and also the* * * * 1-5 root bash /home/xxxxxx/jmeter/Verif..
but no luck -
kal over 9 yearsafter debugging a little further the problem is with $RANDOM (see above)
-
MGodby over 9 yearsHe is running a different script with
at
,jmeter-cron.sh
vsjmeter-cron-randomise.sh
-
kal over 9 yearsyes there are 2 scripts involved here. the flow is cron -->
jmeter-cron-randomise.sh
--->jmeter-cron.sh
-
Volker Siegel over 9 years@MGodby I see, didn't notice - so he should post that script. By the way, that explains the wrong line number in the error: the
%
is iin line 8, not 7. So I suspect the other script is similar, with an%
expression in line 7. -
Volker Siegel over 9 years@kal can you confirm the line number? Maybe we do not need the other script actually.
-
kal over 9 yearshurray! the problem is resolved by directly editing the /etc/crontab to use
SHELL=/bin/bash
-
kal over 9 yearsthanks for your time guys but i am not sure why its worked with the system wide crontab file?
-
Volker Siegel over 9 yearsOh, I thought you had said you tried that already a while ago?! But looking closely at the comments on the other question, you did not confirm with "i tried the SHELL=/bin/bash in the crontab", but "i tried the SHELL=/bin/bash in the crontab entry"... tricky...
-
Volker Siegel over 9 yearsYou can not use the user column in a different crontab - only in the system crontab.
-
Volker Siegel over 9 years@kal So it was the personal crontab of usere root? But then, it should have failed before even starting your script, when trying ro run the command
root
, right? -
kal over 9 years@VolkerSiegel you can have root user in the root user's crontab file (ie. crontab -e once your logged in as root) but the root user also uses the /etc/crontab or thats what i understands from the docs at link. For some reason the $RANDOM was not being resolved because the root users crontab entry for bash shell etc. were not being honoured and the /etc/crontab are.
-
Andrea Corbellini over 8 years@kal: you should include the output you get from cron. By reading the question, it seems like the error you are getting is
arithmetic expression: expecting primary
. But by reading your comments, it seems you are getting another error -
Andrea Corbellini over 8 yearsFWIW, the crontab manpage doesn't say that the script is executed by
/bin/sh
; it says that the command is executed by/bin/sh