Root crontab not running

For some reason, my root crontab does not seem to be running.

Trying to reboot the device every night at midnight.

Should be the following as root:

crontab -e

Then add:

0 0 * * * /sbin/shutdown -r now

When I test using some values close current time, nothing happens. I installed NTP and made sure the time zone is correct. I am also specifying using the 24-hour clock. For example, to test this line right now (5:35 PM) I try entering the following:

36 17 * * * /sbin/shutdown -r now

I have checked the time with date -R. The time for the crontab to run comes and goes and the system does not reboot. What am I missing here?


Solution 1:

I have three solution suggestions for you.

  1. Invoke the crontab with crontab -e -u root

  2. Make sure that you have an empty line at the end of the cronjob file, meaning that every line ends with a newline.

  3. You might need to redirect the output to devnull: shutdown -r now > /dev/null

Here are two helpful webpages for cronjobs:

CRON Tester

CRON Generator

You can also handle the cronjobs neatly with webmin.

Other than that, you have at least two more ways for restarting your computer at midnight.

One is to run the shutdown command as a script automatically at login but with specific time as a parameter instead of "now":

shutdown -r 00:00

However, this will yield a broadcast message of upcoming shutdown at every login (might not be a bad thing at all). Well you can also make this be run at boot time by adding the script in init.d, still yielding the message, though.

Another is to use atcommand:

at 0am

Enter command shutdown -r now and save it with ctrl+d or do a script for the command and do:

at -f restart_script.sh 0am

Hope these help you to get the result you wanted.

Solution 2:

System Cron jobs are listed in /etc/crontab file. Therefore editing this file directly will help you out to run the reboot command as root.

therefore,

$ sudo vi /etc/crontab

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
  23 20 *  *  *  root  shutdown  -r  now

make sure you check the Cron log file after editing the crontab as it will let you know if the cron was installed successfully.

I have tested it and it worked for me. Restarted my system at 8:23 PM

Good LUCK!