Ubuntu – Root crontab not running

crondebianroot

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?

Best Answer

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.

Related Question