Using the terminal or a bash file, how can I configure Ubuntu to reboot every day at (say for instance) 3 AM?

In other words, I want to schedule an automatic reboot through the terminal.

I haven't been able to find anything related to this on Google or AskUbuntu, and I'm fairly new to Ubuntu in general (just got my new Raspberry Pi).

up vote 12 down vote accepted

Edit the crontab entries using crontab -e command (by default this will edit the current logged-in users crontab) and add the following line:

0 3 * * * echo $PASSWD | sudo -S reboot
#change $PASSWD with your password

Save the file and check the new crontab entry with crontab -l.

If you want to use only:

0 3 * * * sudo reboot

as crontab, this doesn't work normally and you should check this post to see how can you make it to work.

Or, simple add the crontab to the root user's crontab file offering the complete path for the reboot command using sudo crontab -e:

0 3 * * * /sbin/reboot
  • Couldn't you put this in root's crontab and not have too worry about the echo $PASSWD – dan08 Jul 31 '13 at 15:21
  • I didn't know this. Is this a bug of cron or is it normal ? I mean, if you edit the crontab as root, then the job will executed only if you are logged in as root ? I think this is wrong. (or should be). – NickTux Jul 31 '13 at 15:44
  • @RaduRădeanu That is not true. Root cronjobs will run without being logged in as root. – OrangeTux Jul 31 '13 at 16:03
  • @OrangeTux You have right, sorry, I said that because I tested without to give the complete path for reboot command. Otherwise will not work. See my new edits. – Radu Rădeanu Jul 31 '13 at 16:39
  • @NikTh I was wrong, see the above comment. – Radu Rădeanu Jul 31 '13 at 16:40

Note that putting your clear-text password in a text file is not a good idea, so it's best to have this job run as root from the get-go. Usually, rather than editing root's crontab via the crontab command, which leaves the entries in /var/spool/cron/crontabs, a somewhat cryptic location, I prefer to enter them explicitly in /etc/cron.d. Entries in cron.d are run as system crontab entries, are treated as config files so they should survive system reboots, updates and upgrades, and you can explicitly specify the running user:

echo "0 3 * * * root /sbin/shutdown -h 5 'System will reboot in 5 minutes'" | sudo tee /etc/cron.d/reboot-at-3-am

If you don't need a specific time, but rather, just want the system to reboot once daily, add an executable or script in /etc/cron.daily and it will be automatically run at a predetermined time (6:25 AM system time by default):

echo "/sbin/shutdown -h 5 'System will reboot in 5 minutes'" | sudo tee /etc/cron.daily/reboot-me

Notice that rather than just rebooting the system without warning, I'm setting a 5-minute warning, so if anyone is logged in, they have a chance to save their work, or even interrupt the shutdown with sudo shutdown -c, rather than having the system pulled off from under them. You can adjust these accordingly, if you want to give more ample warning (for instance, use shutdown -h 60 and run the command at 2:00 AM and you'll give users a generous 1-hour warning).

This is based on my past experience; at some point you will be logged in working when the crontab entry runs, and if it just reboots without warning you'll be a very sad panda.

Why do you want - or need to do this? Whilst it's pretty simple to do, there's almost certainly a better solution with Linux which doesn't involve a reboot. Unlike Windows, Linux based OS's don't require reboots for almost anything except kernel upgrades.

If you explain what you are trying to achieve we can probably help you better.

  • 3
    He wants to reboot the computer every night so it will reboot – Michael Butler Aug 6 '13 at 20:22

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.