53

I'm new in Linux and I want to schedule a reboot at midnight. How should I do it?

Edits:

  • I'm sorry I didn't put the complete details. I want a reboot every 3rd Saturday of the month at 23:30.

  • I don't know what's wrong but I cannot find crontab. What I have is cron.d; cron.daily; cron.weekly; cron.monthly;

I'm sorry for the noob question. Pls help me. Thanks.

13
  • 2
    The real question is why you'd want to be doing this regularly, do you have a leaky application or something? If you're scheduling regular reboots to solve a problem then you've probably got a bigger problem... Jun 27, 2010 at 14:05
  • 1
    Do you want it to reboot every night? Jun 27, 2010 at 14:33
  • 7
    I'm not voting down, because it's sort of a policy decision (and I think nightly reboots are a bit excessive), but regularly scheduling reboots across an infrastructure are a decent way of ensuring that machines have up to date kernels and all services running are patched. I've heard a 3 month uptime limit as being a good middle ground. Jun 27, 2010 at 14:37
  • If this was a comment on the question, rather than an answer, I would upvote it. Actually I would change my mind, because the question was ambiguous about whether this was a one-time or regular thing. :)
    – intuited
    Jun 27, 2010 at 14:37
  • 5
    /etc/crontab entry like 30 23 15-21 * 6 /sbin/shutdown -r now will reboot at 23:30 on the 3rd Saturday of every month.
    – Chris S
    Jun 28, 2010 at 17:53

6 Answers 6

69

Type shutdown -r 0:00 and it will reboot at midnight.

If you want to reboot each night, add a cron entry using crontab -e as root to run shutdown -r each midnight

@midnight shutdown -r now
7
  • 4
    A time argument is mandatory, you can use now.
    – Tobu
    Jun 27, 2010 at 12:38
  • No ... if he want a one time command and want to type the command now, he should add the time !
    – radius
    Jun 27, 2010 at 12:40
  • 5
    @No, it needs a time argument such as now in the crontab line. Jun 27, 2010 at 13:16
  • Oups yes sorry, I misread Tobu comment and think he tells 0:00 was useless in the first command... I edited to correct (and Massimo answers too)
    – radius
    Jun 27, 2010 at 13:21
  • 6
    Note that issuing shutdown -r 22:00 then closing the terminal will cancel the reboot command. nohup shutdown -r 22:00 & for maintaining the reboot command after closing the terminal window.
    – a coder
    Jun 2, 2016 at 19:03
18

Another option is the at command, available on many Linux distributions. See the man page for more info, but the general syntax for your purpose would be:

echo "reboot" | at 0000 jun 27

To quote the OS X man page:

at - executes commands at a specified time

Sound like what we're talking about. ;)

0
17

Using crontab.

http://en.wikipedia.org/wiki/Crontab

Adding this entry to /etc/crontab should do:

0 0 * * * /sbin/shutdown -r now
5
  • 3
    shutdown needs a time argument such as now Jun 27, 2010 at 13:19
  • 1
    This will do the reboot every night. I am not sure if this is what he wants.
    – cstamas
    Jun 27, 2010 at 14:39
  • 1
    The original question made it sound like he wanted it rebooted every night. He actually wants it rebooted every 3rd Saturday, which would still be easiest with a crontab
    – Chris S
    Jun 28, 2010 at 17:51
  • i dont know what's wrong but i cannot find the crontab..? what i have is the cron.d;cron.daily; cron.weekly; cron.monthly; I'm sorry for the noob question.Pls help me. thanks
    – klauriens
    Jun 30, 2010 at 11:00
  • It depends on the distribution you're using, but usually you should find a file called "crontab" in your system's /etc directory. You should also be able to examine the current crontab using the command "crontab -l", and to edit it using "crontab -e".
    – Massimo
    Jun 30, 2010 at 13:12
2

As far as I know, you cannot use cron to schedule tasks for "last Friday of each month" or "third Thursday in each month". What you can do, however ugly it seems, is to have a script run every Saturday at 23:30 and then have this script determine if this particular Saturday is the third Saturday of the week (can be done using date and maybe cal commands).

I hope this helps. I have not found an elagant solution to this problem. I found this thread, because I was searching for a solution for the same problem.

7
  • 3
    the comments above of "30 23 15-21 * 6 /sbin/shutdown -r now" will work. It'll basically only run on the third week of the month, but only when it's also saturday
    – Sirex
    Mar 6, 2012 at 14:47
  • I do not think this cron line works as you think it does.
    – ervingsb
    Mar 8, 2012 at 9:36
  • I created the following two lines: "30 23 1-7 * 4 date" and "30 23 1-7 * 3 date". I got two mails last night both saying: "Wed Mar 7 23:30:02 CET 2012"
    – ervingsb
    Mar 8, 2012 at 9:37
  • the last field matches the day of the week, with zero being sunday. the 4 cronjob should not have matched on a wednesday.
    – Sirex
    Mar 9, 2012 at 15:35
  • That is what I meant. The rule you wrote do not mean what you think it means. It will trigger the job every day for a week. No matter the weekday.
    – ervingsb
    Mar 9, 2012 at 19:47
1

1) at the command line type which reboot
2) once you know where reboot is located (usually /sbin/reboot) cd into one of the the directories in /etc/cron.daily , /etc/cron.weekly , /etc/cron.hourly etc... ie cd /etc/cron.weekly

3) create a file (using nano or vim) call it zzreboot and add the following lines:

#!/bin/sh
/sbin/reboot

The reason you want to call the file zzwhatever is to make sure it's the last job called after all other jobs. To make sure just do an ls -l in the directory and verify it's the last file.

0

I know this is an old question, still I would like to answer it. On your terminal logged in as root follow below steps:

  1. #crontab -e this would open the cron file to write your cron jobs
  2. add a new line like30 23 15-21 * sat /path/to/reboot. This cron job would reboot the system every 3rd sat of the month at 11.30 pm.
  3. save and exit

You must log in to answer this question.

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