3

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
13
  • Does your script begin with a shebang, i.e. #!/bin/bash ?
    – MGodby
    Oct 23, 2014 at 15:06
  • 1
    yes it does #!/bin/bash
    – kal
    Oct 23, 2014 at 15:22
  • i 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.
    – kal
    Oct 23, 2014 at 15:23
  • This script will always be ran with "bash", then, so long as it is present on the system.
    – MGodby
    Oct 23, 2014 at 15:24
  • This will probably require more information about your script if you want help troubleshooting its behavior.
    – MGodby
    Oct 23, 2014 at 15:25

2 Answers 2

4

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
# ...............^^^^
5
  • 1
    already tried the SHELL=/bin/bash in the crontab entry and also and but no luck.
    – kal
    Oct 23, 2014 at 15:45
  • meant 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
    Oct 23, 2014 at 15:55
  • after debugging a little further the problem is with $RANDOM (see above)
    – kal
    Oct 23, 2014 at 16:14
  • @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 Nov 8, 2015 at 16:02
  • FWIW, the crontab manpage doesn't say that the script is executed by /bin/sh; it says that the command is executed by /bin/sh Nov 8, 2015 at 16:07
0

The crontab does run the script with bash;

But you're running it recursively with at.

And at uses /bin/sh.

Because of an earlier problem, the time specification for at is now+0min - so it's running again just at the end.

That run fails, so there is no loop, at least.

The problem leading to the now+0min is that n % 1 is 0, so $RANDOM % 1 = 0, and rmin = 0.

Add a second line

set -x

to the script for better logging, you'll the last line with the 0 timespec.

10
  • 1
    He is running a different script with at, jmeter-cron.sh vs jmeter-cron-randomise.sh
    – MGodby
    Oct 23, 2014 at 16:29
  • yes there are 2 scripts involved here. the flow is cron --> jmeter-cron-randomise.sh ---> jmeter-cron.sh
    – kal
    Oct 23, 2014 at 16:33
  • @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. Oct 23, 2014 at 16:34
  • @kal can you confirm the line number? Maybe we do not need the other script actually. Oct 23, 2014 at 16:35
  • hurray! the problem is resolved by directly editing the /etc/crontab to use SHELL=/bin/bash
    – kal
    Oct 23, 2014 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.