138

I'm having some periodic issues running a particular application, XBMC. If I use XBMC regularly I don't seem to have any issues but if I leave it unattended for more than about 12 hours I need to reboot to get it working again.

I know a scheduled reboot is NOT the answer but until I can figure out the problem I need to schedule a reboot each morning so my wife can use it if I'm away and it doesn't get thrown out the window :)

Any takers?

6 Answers 6

222

I'd use cron (should already be installed):

Edit crontab:

sudo crontab -e

The first time you might have to choose your preferred editor (like nano)

Insert a line like

0 4   *   *   *    /sbin/shutdown -r +5

at the bottom. Explanation:

m      h    dom        mon   dow       command
minute hour dayOfMonth Month dayOfWeek commandToRun

so the line

  0 4   *   *   *    /sbin/shutdown -r +5

would reboot your system every day at 4:05am. (4:00am + 5 minutes)

Ctrl+X, Y, Enter should get you out of crontab (if using nano)

Note: you might have to run crontab -e as root, because shutdown needs root. crontab -e opens a file in /tmp instead of the actual crontab so that it can check your new crontab for errors. If there are no errors, then your actual crontab will be updated.

8
  • 1
    Thanks for the info. The only thing I had to do was change the command to /sbin/shutdown to get it to work for me.
    – Richard Edwards
    Nov 18, 2010 at 21:17
  • 3
    this didnt work for me. using both crontab -e and sudo crontab -e saves the file to a temporary location when you go to exit (e.g. /tmp/crontab.Y88XQl/crontab) and the task does not run. i was forced to edit /etc/crontab directly
    – wal
    Jul 21, 2019 at 0:32
  • 2
    @wal "crontab -e opens a file in /tmp instead of the actual crontab so that it can check your new crontab for errors and prevent you from overwriting your actual crontab with those errors. If there are no errors, then your actual crontab will be updated." unix.stackexchange.com/questions/197504/…
    – Christh
    Jul 24, 2019 at 6:36
  • 13
    Why do you add +5?
    – birgersp
    Nov 11, 2019 at 7:31
  • 1
    Maybe it will help someone: I had to use sudo before reboot command in the file opened by sudo crontab -e in Linux Mint (but I'm not sure if it makes any difference in this case).
    – kcpr
    May 6, 2020 at 19:05
24

Adding this to /etc/cron.daily/zz-reboot should work:

#!/bin/sh
shutdown -r now

And sudo chmod a+x /etc/cron.daily/zz-reboot. The "zz" prefix will force it to run last out of all the other scripts in that directory. Check /etc/crontab to see what time of day that will actually happen:

grep daily /etc/crontab | awk '{print $2 ":" $1}'

If that won't work, then a "regular" cron entry can work too, via sudo crontab -e

MINUTE HOUR * * * shutdown -r now

And finally, if you want to just do one-off reboots, you can use at:

echo "shutdown -r now" | sudo at 04:30
4
  • as far as I know this will reboot your system the same time you created this file every day, please correct me if I'm wrong
    – sBlatt
    Nov 17, 2010 at 21:22
  • 1
    Not quite: it will reboot once per day at whatever time cron.daily runs, which is a bit unpredictable. If you don't have anacron installed, it's 06:25 local time, which might be ok (or might be awful if you're an early riser). If you do, which I think is the default, it could well be 10 minutes after the machine turns on for the first time each day, which would be infuriating. At any rate, cron.daily is not a good fit for this.
    – poolie
    Nov 17, 2010 at 21:27
  • cron.daily run every night at 6:25 on my system. This is usually set to a random minute between 06:00 and 06:59. See your /etc/crontab for the time on your system
    – elzapp
    Nov 17, 2010 at 21:30
  • 1
    The biggest problem with using cron.daily for this is that it might interfere with other jobs in cron.daily.
    – elzapp
    Nov 17, 2010 at 21:31
5

I have been working with cronjobs for about a month at my work and scheduling poweroff, and reboot. It's very simple. I know this was asked about 5 years ago, but if anyone still has problems, you can use this method and you will be set.

open the terminal (ctrl+T)

sudo nano /etc/crontab

scroll all the way to the bottom and enter the below command

00 6 * * * root reboot 

this is set for reboot at 6am everyday, and press enter

If you want to schedule poweroff at 11pm everyday you can enter

00 23 * * * root poweroff

I still need to figure out how to poweron a machine using cronjob when it's down. I will edit this answer once i figure it out.

P.S. this is my first ever answer posting on any forms; hope it helps someone!! :D

3
  • 6
    There is an answer with the same content from 5 years ago with more detail and automatic syntax verification (through crontab -e). Aug 1, 2015 at 18:00
  • 4
    Do not forget to restart cron : sudo /etc/init.d/cron restart
    – brcebn
    May 12, 2016 at 7:23
  • I know this is many years after the original message, however, its the first time I've come across it. The above line about using CRON to power on a server when its been powered down made me chuckle.
    – Jim Grant
    Nov 23, 2021 at 9:12
4

Consider

0 6 * * * sudo shutdown -r     

This will a 6 am reboot every day. I like this because it allows a 1 minute delay to shut down any other background jobs and warns anyone else that a shutdown is pending.

4

This is an older question, but it comes up as search result and does not contain any info about systemd, so I will add an example on how to do scheduled reboots with systemd. Filenames and unit descriptions in the example are chosen arbitrarily and can be changed.

  1. Add a service unit that restarts the system. To do that, create a new file in /etc/systemd/system/sched-reboot.service and add the necessary configuration into it. The following service config will run a forced reboot through systemctl whenever started.

     [Unit]
     Description=Scheduled Reboot
    
     [Service]
     Type=oneshot
     ExecStart=/usr/bin/systemctl --force reboot
    
  2. Add a timer, that will start the reboot service on a schedule. Create a new file in /etc/systemd/system/sched-reboot.timer. The following config for the timer will run the reboot service daily at 4AM

     [Unit]
     Description=Reboot Scheduling
    
     [Timer]
     OnCalendar=*-*-* 4:00:00
    
     [Install]
     WantedBy=multi-user.target
    
  3. (Optional) Run systemd-analyze verify /etc/systemd/system/sched-reboot.* to check for errors in the config files. If the return is empty, the files are in order.

  4. Enable and start the timer by running sudo systemctl enable sched-reboot.timer and sudo systemctl start sched-reboot.timer.

  5. (Optional) Run systemctl list-timers to see all active timers as well as their next and last start information.

For more info about timers and the syntax for setting calendar events (as in OnCalendar in the example) have a look at the ArchWiki-section about Timers

2

You should create a script using the directions given by Kees Cook...

You can just copy and paste the information below in any text editor and create the zz-reboot file in the directory suggested.

After that just remember to right click on the file and assign it execution permission. If you feel like doing in using terminal just:

sudo chmod +x /etc/cron.daily/zz-reboot

To understand better what you're doing remember that in /etc folder you generally find configuration files and there you can find cron.hourly, cron.daily and other cron folders. Cron takes care of executing applications and script at a certain time.

If you want to be strict about the reboot time just digit

sudo crontab -e

so you can edit the crontab for the root user.

If you feel better doing it graphically you can install from the Software Center gnome-schedule. If you want to modify the gnome-schedule for root user ensure that you run it from terminal:

gksudo gnome-schedule

Have fun playing around! :)

p.s.: great point sBlatt! I was wondering if there's any way to force cron.daily execution time manually.

1
  • In /etc/crontab there is an entry to adjust time, but afaik, if the computer is down at this time, it will execute some minutes after the computer started
    – sBlatt
    Nov 17, 2010 at 21:46

You must log in to answer this question.