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 sh fails with following error:
> jmeter-cron-randomise.sh: 7: jmeter-cron-randomise.sh: arithmetic
> expression: expecting primary: " % 1 "
Having googled the problem, it seems like standard shell doesn't have the same math operators, like % (modulus), as bash. I'm Not sure why the cron job is failing in the script? I am assuming it is because it's 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.
cronrun the command with/bin/sh? It looks like it probably does because the error is what you'd like get if you ran the script withshinstead ofbash(as in:sh /home/…/jmeter-cron-randomise.sh). And on Ubuntu,/bin/shis oftendash, notbash. To fix, usebash /home/…/jmeter-cron-dandomise.sh >> …in your crontab entry.bash /path/of/script.sh. Thisbashshould be the output ofwhich bash, that is, the full path of the binary.head -n 1 /home/xxxxxx/jmeter/VerificationService-0.0.1-SNAPSHOT/jmeter-cron-randomise.sh | cat -vE? This will show the first line in a visually unambiguous way, to help determine whether e.g. a byte order mark is invalidating your shebang.