13

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?

3
  • Just to make sure, you are closing the crontab after editing it, right? Also, how did you "run as root"? What is the output of sudo crontab -l | grep -v '#'?
    – terdon
    Nov 25, 2014 at 1:16
  • Why is this tagged debian? If this is about Debian and not Ubuntu, please ask on Unix & Linux.
    – muru
    Nov 25, 2014 at 1:46
  • 1
    Because debian is at the core of Ubuntu I believe.
    – Atomiklan
    Nov 26, 2014 at 16:38

2 Answers 2

15

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:

CronDrive tester

Cron expression 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

2
  • 1
    The solution was to output to dev null. I wonder why?
    – Atomiklan
    Nov 26, 2014 at 16:36
  • 1
    It is because cron job has its own environment which does not have such a standard input/output system you'd expect and the process fails because it is trying to stream messages into a missing output pipe. Cron job is actually a lot different thing compared to running some command in terminal as any user. Glad to hear that you got it working after all.
    – Ahti Komu
    Nov 27, 2014 at 2:34
1

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!

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .