64

I tried to set up a root cron job to run a Bash script as root, to run at minute 7,37, every hour, every day of month, every month. This script is located in /usr/bin and named tunlrupdate.sh. It updates the DNS of Tunlr.

$ ls -l /usr/bin/tunlrupdate.sh 
-rwxr-xr-x 1 root root 2133 Sep 24 15:42 /usr/bin/tunlrupdate.sh

This Bash script is available here.

When invoked the script writes what's happening in a log located in /var/log/tunlr.log

To add this root cron job I used the standard for root's crontab

sudo crontab -e

And inserted these 2 lines at the end. I expect cron to run the script as root.

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

A later command sudo crontab -l confirmed that the cron job has been inserted.

I did reboot Ubuntu and was checking in the log file if the cron job was launched properly. However there is nothing in the logfile /var/log/tunlr.log meaning the job was never successfully launched.

I did check that if I run the script from the command line

sudo /usr/bin/tunlrupdate.sh

then the logfile is updated accordingly.

Why is this cron job not running as planned in my system?

UPDATE 1 : All the proposed solutions so far do not work. I thank Olli for a CLI to list the system log sudo grep CRON /var/log/syslog. However I did get a CRON error

CRON[13092]: (root) CMD (  [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ]
&& find /var/lib/php5/ -depth -mindepth 1 -maxdepth 1 -type f -cmin +$(/usr/lib/php
/maxlifetime) ! -execdir fuser -s {} 2>/dev/null \; -delete)

with the suggested PATH= insertion & use of absolute path from root for functions in the script or without this suggested solutions here. I still get this error.

After some searching I pinpointed the error in the file /usr/lib/php5/maxlifetime as is explained here: Change #!/bin/sh -e --> #!/bin/sh -x

Then listing the CRON error log in my system

sudo grep CRON /var/log/syslog
Feb 11 18:07:01 Marius-PC CRON[14067]: (root) CMD (root /usr/bin/tunlrupdate.sh)
Feb 11 18:07:01 Marius-PC CRON[14066]: (root) MAIL (mailed 1 byte of output; but got
status 0x00ff, #012)

I still don't get the bash script executing. This time no error is shown in the log. To get assurance this was not the content of the script I reduced the script to the following 3 lines:

#!/bin/bash
LOGFILE=/var/log/tunlr.log
echo $LOGFILE >> $LOGFILE

I still don't get the cron job through. Nothing is written in the log file. So even may be an empty script will not run in cron ? I don't get it. I am know trying a script reduced to these 2 lines:

#!/bin/bash
exit 0

And still the same error log. The cron script does not go through ...

Zanna
  • 72,312
Antonio
  • 1,656

6 Answers6

111

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
Zanna
  • 72,312
Guillaume
  • 2,151
14

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].

Antonio
  • 1,656
5

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.

Alexis Wilke
  • 2,787
2

Cron error messages are usually - by default - sent via email. You can check whether there's email for root with sudo mail, or by just checking contents of /var/mail/root, e.g sudo less /var/mail/root.


If email messages does not help, also check /var/log/syslog:

sudo grep CRON /var/log/syslog

As Alexis Wilke already said, cron have different mechanism for setting environment variables.

Your script needs

PATH=/sbin:/bin:/usr/bin

to the crontab. HOME shouldn't be necessary. You should use absolute paths in your scripts, e.g /bin/date instead of date. You can find proper paths for each command with which command_name, e.g

$ which date
/bin/date
Olli
  • 9,129
0

You state in your question that you reduced the script to:

#!/bin/bash
LOGFILE=/var/log/tunlr.log
echo $LOGFILE >> $LOGFILE

I have had problems with this before. It turned out to be the first line. Instead of #!/bin/bash I tried #!/bin/sh and that worked (for me, at least).

Eliah Kagan
  • 119,640
0

You can add this line in your script. So after you check the cron logs and comprove your job was executed you can get the the same $PATH of crontabs had.

/bin/echo $PATH > /root/path.txt

And probably the best thing you can do to diagnose issues in cron scripts is get all the environment variables of SO with env command in your script. So just add this line to your script. Then you can analyze the output allEvnVars.txt

/usr/bin/env > /root/allEvnVars.txt

Another trick is to direct the output of the script to some place. Adding the /root/log.log. This way all the output of the script will be maintained in /root/log.log

07,37 * * * * root /usr/bin/tunlrupdate.sh  > /root/log.log

Also you can schedule the script to run each min to facilitate the tests and checks.

*/1 * * * * root /usr/bin/tunlrupdate.sh  > /root/log.log
Zanna
  • 72,312