Cannot run bash script from crontab when it works from command line bash

25,408

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
# ...............^^^^
Share:
25,408

Related videos on Youtube

kal
Author by

kal

Updated on September 18, 2022

Comments

  • kal
    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
      MGodby over 9 years
      Does your script begin with a shebang, i.e. #!/bin/bash ?
    • kal
      kal over 9 years
      yes it does #!/bin/bash
    • kal
      kal over 9 years
      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.
    • MGodby
      MGodby over 9 years
      This will probably require more information about your script if you want help troubleshooting its behavior.
    • kal
      kal over 9 years
      #!/bin/bash echo Running the jmeter-cron-randomiser script script="/home/xxxxx/jmeter/VerificationService-0.0.1-SNAPSHO‌​T/jmeter-cron.sh" min=$(( 1 * 1 )) rmin=$(( $RANDOM % $min )) echo "min = ${min}"; echo "rmin = ${rmin}" at -f "$script" now+${rmin}min '
    • MGodby
      MGodby over 9 years
      Please update your original question and use appropriate formatting for readability.
    • kal
      kal over 9 years
      i 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
      MGodby over 9 years
      Upon further inspection, I think that $RANDOM does not exist in your script because cron launched from a non-bash shell like you said.
    • kal
      kal over 9 years
      yes thats the crux of the problem. when I run from sh $RANDOM is not resolved but is from bash
    • Volker Siegel
      Volker Siegel over 9 years
      What is the exact name of the crontab file? /etc/crontab?
    • Alaa Ali
      Alaa Ali over 9 years
      If 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 word root 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, like echo $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
    kal over 9 years
    already tried the SHELL=/bin/bash in the crontab entry and also and but no luck.
  • kal
    kal over 9 years
    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
    kal over 9 years
    after debugging a little further the problem is with $RANDOM (see above)
  • MGodby
    MGodby over 9 years
    He is running a different script with at, jmeter-cron.sh vs jmeter-cron-randomise.sh
  • kal
    kal over 9 years
    yes there are 2 scripts involved here. the flow is cron --> jmeter-cron-randomise.sh ---> jmeter-cron.sh
  • Volker Siegel
    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
    Volker Siegel over 9 years
    @kal can you confirm the line number? Maybe we do not need the other script actually.
  • kal
    kal over 9 years
    hurray! the problem is resolved by directly editing the /etc/crontab to use SHELL=/bin/bash
  • kal
    kal over 9 years
    thanks for your time guys but i am not sure why its worked with the system wide crontab file?
  • Volker Siegel
    Volker Siegel over 9 years
    Oh, 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
    Volker Siegel over 9 years
    You can not use the user column in a different crontab - only in the system crontab.
  • Volker Siegel
    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
    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
    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
    Andrea Corbellini over 8 years
    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