52

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.

Chris W.
  • 103
  • 1
  • 1
  • 10
klauriens
  • 541
  • 1
  • 4
  • 6
  • 1
    Do you want it to reboot every night? – Matt Simmons Jun 27 '10 at 14:33
  • I'm sorry I didn.y put the complete details. I want a reboot every 3rd Saturday of the month at 23:30 – klauriens Jun 28 '10 at 17:08
  • 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 '10 at 17:53
  • 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 '10 at 11:00
  • You don't need to edit a file for crontab, just use the command "crontab -e" as root and it will open a text editor for you. By "as root", we mean either type "su -" first and then enter the root password that was set up when the server was set up, or just type "sudo crontab -e" and enter your password. Which to use depends on your version of Linux. – Paul Kroon Jun 30 '10 at 11: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... – JamesHannah Jun 27 '10 at 14:05
  • 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. – Matt Simmons Jun 27 '10 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 '10 at 14:37
  • @Matt Simmons: so you don't keep track of the patches you apply and reboot when its required? – symcbean Jun 27 '10 at 15:14
  • @symcbean - Sure, but if you reboot everything at the same time, then...well, you lose service availability AND it's chaos. I can see advancing the schedule because of a critical vulnerability, but every small kernel upgrade doesn't mean "install-it-now-or-dire-circumstances-ensue". – Matt Simmons Jun 28 '10 at 01:29
  • @Matt Simmons: If you can't reboot a server without losing service, then you've got architecture problems you really should fix. – symcbean Jun 28 '10 at 11:34
  • symcbean: Yes, however if you do updates and reboots across the board, all at once, you lose service because the other members of the service group go down too. I think we're arguing about the same thing ;-) – Matt Simmons Jun 28 '10 at 11:40
  • First, check to see what time it is on the system you want to reboot, using the `date` command. Is its clock in sync with your local time? If not, you may want to take that into consideration. – LonnieBest Sep 28 '21 at 09:42

6 Answers6

68

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
Eric Fossum
  • 225
  • 3
  • 11
radius
  • 9,633
  • 25
  • 45
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. ;)

sorin
  • 8,016
  • 24
  • 79
  • 103
Zayne S Halsall
  • 1,982
  • 15
  • 19
17

Using crontab.

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

Adding this entry to /etc/crontab should do:

0 0 * * * /sbin/shutdown -r now
radius
  • 9,633
  • 25
  • 45
Massimo
  • 70,200
  • 57
  • 200
  • 323
  • 3
    `shutdown` needs a time argument such as `now` – Dennis Williamson Jun 27 '10 at 13:19
  • 1
    This will do the reboot every night. I am not sure if this is what he wants. – cstamas Jun 27 '10 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 '10 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 '10 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 '10 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.

ervingsb
  • 395
  • 1
  • 6
  • 16
  • 2
    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 06 '12 at 14:47
  • I do not think this cron line works as you think it does. – ervingsb Mar 08 '12 at 09: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 08 '12 at 09: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 09 '12 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 09 '12 at 19:47
  • hmmm, appears you're right. that's pretty crappy isn't it ! – Sirex Mar 12 '12 at 10:00
  • Yeah. Very. So what to do about this? I can hardly imagine that Linux and Unix admins do not have this scheduling need. – ervingsb Mar 12 '12 at 13:56
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