Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I tried to setup 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 is used to update 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 write 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, root is meant 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 googling 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 ...

share|improve this question
    
If you want it to bee a "root" cronjob you have to be root then you type crontab -e. Also you just have to login as root (console type "su root") and then crontab -e (sudo is not needed in this case). –  Wolfgang Vogl Feb 11 at 18:05
    
Well. I don't see the point of your answer ? Typing $ sudo crontab -e did the job as reported by $ sudo crontab -l , i.e. the line describing the new job has been added to the root's cron. As per se the job is not present in the user cron e.g. $ crontab -l do not show any cron job was added here. –  Antonio Feb 11 at 18:44
    
@WolfgangVogl he used "sudo" which works as expected. –  Alexis Wilke Feb 11 at 18:45

3 Answers 3

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.

share|improve this answer

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
share|improve this answer
    
Running your suggested grep CRON in /var/log/syslog I got the foolowing Feb 11 14:37:01 Marius-PC CRON[7826]: (root) CMD (root /usr/bin/tunlrupdate.sh) Feb 11 14:37:01 Marius-PC CRON[7825]: (root) MAIL (mailed 1 byte of output; but got status 0x00ff, #012) Feb 11 14:39:01 Marius-PC CRON[7849]: (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/php5/maxlifetime) ! -execdir fuser -s {} 2>/dev/null \; -delete) If I add some definitiion for the PATH in cron these will not affect my system $PATH ? –  Antonio Feb 11 at 20:11
    
That output says it tried to email something but failed. That means, you are not getting that error message to /var/mail/root. You can either fix that, or try with PATH=... –  Olli Feb 11 at 20:14
    
@Antonio, alternatively, use this patched version –  Olli Feb 11 at 20:18
    
Thanks. I know I didn't configure system e-mail and was aware that didn't go through. I already had modified the script to use absolute path for any function called. The last thing was my previous question a PATH definition within crontab will not mess my system $PATH variable ? –  Antonio Feb 11 at 20:37
1  
In the mean time I was busy correcting a bug in the date format from the original script. It was using %m (which is for month NOT minute) instead of %M ... –  Antonio Feb 11 at 20:40
up vote 0 down vote accepted

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

share|improve this answer
    
Aaaah, shit. Obviously that's the problem. If you use /etc/crontab (or other cron files under /etc), you have to specify user, hence, 07,37 * * * * root /usr/bin/tunlrupdate.sh. With user crontabs, that is not the case, as you already noticed. Usually for root either /etc/crontab or /etc/cron.d/<name of your script> is used. –  Olli Feb 12 at 10:18
    
Good point Olli. I agree with you on that. For reference the user crontab is stored in /var/spool/cron/crontabs/user-name or for root /var/spool/cron/crontabs/root . See this page askubuntu.com/questions/216692/where-is-the-user-crontab-stored The containing folder already is and gives the name of the user. –  Antonio Feb 12 at 17:52
    
Well, you shouldn't need that information, as you only should edit crontab files with crontab command (except crontab files under /etc). –  Olli Feb 13 at 17:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.