[SOLVED] Why doesn't cron run sometimes?

  • Just a small post for people like me going nuts for why cron tasks sometimes don't run.

    First, the shebang of your script. I use to have bash scripts that have

    #!/usr/bin/env bash

    If your shebang doesn't look like the following it won't execute (giving exit code 127 to cron who doesn't seem to log this anywhere):

    #!/usr/bin/env sh

    Your script should reside in /usr/local/bin/script.sh
    Also, render it executable

    chmod +x /usr/local/bin/script.sh

    Using the cron packages, it modifies the /etc/crontab file.
    You should not edit this file, but for test sake, you may include a line (put it somewhere between the other lines) that looks like:

    */1     *       *       *       *       root    /usr/local/bin/script.sh

    the */1 says it should run every minute.

  • As I have a bash only script, here's a way to get it executed right.

    Create the following script in /usr/local/bin/script-launcher.sh

    #!/usr/bin/env sh
    echo "Executing the cron" >> /tmp/cron_test
    PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/root/bin /usr/local/bin/obackup.sh /etc/obackup/host_backup.conf
    echo "Return code $0" >> /tmp/cron_test

    In my case, I have to execute /usr/local/bin/obackup.sh and a parameter, so just replace it with whatever bash script you need to execute.
    Then you can add it to the cron package like:

    */1     *       *       *       *       root    /usr/local/bin/script-launcher.sh

    You may see if it's well executed by using

    cat /tmp/cron_test

    If everything went well, comment out the echos.

Log in to reply