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 …
Best Answer
If you want to run a script as a normal user:
And add the line:
If you want to run your script as root:
And add the same line: