Scheduled a computer to reboot everyday at 7AM, not sure if I did it correctly

Running Ubuntu 20.04, I have computers that I want to reboot everyday at 7AM. I followed these instructions, and I thought it was working, but now when I use the uptime command, it will often report longer than 24 hours, which if it reboots daily, it should never exceed.

These were the instructions I used.

To schedule a daily reboot at 7 AM on a Linux system, you can use the cron scheduler. Open your crontab file by running:

crontab -e

Select an editor. To change later, run ‘select-editor’.

  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.tiny
  3. /bin/ed

choose option 1 for the editor

Then, add the following line to reboot everyday at 7 AM:

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

Save the file, and the system will reboot everyday at 7 AM.

In this example:

•   Minute: 0
•   Hour: 7 (7 AM)
•   Day of the month: * (every day of the month)
•   Month: * (every month)
•   Day of the week: * (every day of the week)

This line specifies that the system will reboot every day at 7 AM. Save the file after making this modification.

I did the CTRL X and saved the file (File Name to Write: /tmp/crontab.GO6Qpc/crontab), was back at the terminal prompt and rebooted. Is there anything else I need to do? Did I save it in the correct place? Do I need to add any sudo commands anywhere?

Asked By: Jackjackson


Your, cronjob and logic is correct syntax-wise and you can verify if you succeeded in installing the new crontab for your user with:

crontab -l

… and you should see your added cronjob listed in the output.

That said, a reboot might need to be issued by a user with elevated privileges in some cases like if other users are logged in or an inhibitor is set by a service/application at the time the cronjob is executed and in which cases, CRON invoked as a regular user will fail to execute that command.

Therefore, you should probably be installing such cronjobs to root‘s crontab instead with sudo like so:

sudo crontab -e

… and to list cronjobs for root, use sudo as well like so:

sudo crontab -l
Answered By: Raffa
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.