How to set up a root cron job properly

If you want to run a script as a normal user:

crontab -e

And add the line:

07,37 * * * * /usr/bin/tunlrupdate.sh

If you want to run your script as root:

sudo crontab -e

And add the same line:

07,37 * * * * /usr/bin/tunlrupdate.sh

Well, at last the working solution. In the syslog I saw the repetitive and intriguing:

CRON[18770]: (root) CMD (root /usr/bin/tunlrupdate.sh)

That sounds like root was not recognized as a cmd. As I already used the root's cron by using $ sudo /usr/bin/tunlrupdate.sh. Then I tried with the original script (corrected for a mistake in the date UNIX cmd : %m which is month was used for minutes which is %M) the following (which removes the root from the cron line):

$ sudo crontab -e
# check for updated Tunlr DNS every 30 minutes at the hour + 7 mn and hour + 37 mn
07,37 * * * * /usr/bin/tunlrupdate.sh

This turned out to be the final solution. [Although I found scores of literature stating the erroneous line with root in the cron line. That was a mistake].


One "problem" with cron is that lack of environment variables (for obvious security reasons). You are probably missing PATH and HOME. You can define those in the script directly or in the crontab file.

# check for updated Tunlr DNS every 30 minutes at the hour + 7 mn and hour + 37 mn
PATH=/usr/bin
07,37 * * * * root /usr/bin/tunlrupdate.sh

You'll have to test until all the necessary variables are defined as required by the script.

Tags:

Cron