48

I'd like to be able to schedule a server reboot at a specific time, but not regularly. How can I do this without futzing with adding and removing cron entries?

jldugger
  • 14,342
  • 20
  • 77
  • 129

5 Answers5

88

If it is one-time deal, you can use shutdown command with -r as argument. Instead of using shutdown now, you can add time as parameter. Example:

shutdown -r 12:30

This also works with times in the early morning - so if it's 15:55 now, you can reboot the server at 3:15am tomorrow morning. You can also add & to put the command in background so you can log-off without killing the shutdown command. Example:

shutdown -r 03:15 &

In case, to cancel that reboot: run this:

shutdown -c
Valerio Bozzolan
  • 314
  • 2
  • 15
Josip Medved
  • 1,734
  • 2
  • 18
  • 18
  • 7
    -r is critical otherwise you will be hitting a power button somewhere to bring it back up. – egorgry Aug 22 '09 at 21:51
  • 16
    This also works with times in the early morning - so if it's 15:55 now, you can use 'shutdown -r 03:15 &' to reboot the server at 3:15am tomorrow morning. (the '&' shunts the command the background so you can log-off without killing the shutdown command) – Andrew Aug 25 '09 at 14:55
  • 3
    This has the advantage/drawback (depending on your use case) of alerting everyone every hour or so via a broadcast message of the upcoming reboot. – Klaas van Schelven Aug 05 '14 at 08:33
  • To cancel this schedule just `shutdown -c` without any other argument – Valerio Bozzolan Jul 05 '23 at 09:53
20

According to the man page: /sbin/shutdown [-t sec] [-arkhncfFHP] time [warning-message] found at --> http://unixhelp.ed.ac.uk/CGI/man-cgi?shutdown+8

Load of options to choose from but, to answer your question.

To reboot in 5 minutes: /sbin/shutdown -r 5 "reboot in five minutes"

To reboot at exactly 11:00 P.M.: /sbin/shutdown -r 23:00 "rebooting at 11:00 P.M."

NOTE: your message will be broadcast to all active terminals / sessions.

ForgeMan
  • 401
  • 2
  • 8
8

the at command is what you want.

at 5:00pm 
do
cd /
/full/path/to/init 6
done

at -l will list the at cmds

jldugger
  • 14,342
  • 20
  • 77
  • 129
egorgry
  • 2,871
  • 2
  • 23
  • 21
  • 6
    I would use `shutdown` instead of `init`. It's not necessary to do the `cd` or the `do`/`done` (which would probably produce an error). – Dennis Williamson Aug 22 '09 at 22:17
  • really? I've used this for over 8 years and I've never has an issue. hpux. solaris, linux 2.2 - 2.6 – egorgry Aug 22 '09 at 22:47
  • 1
    I personally prefer `init 6` myself; had intermittent issues with `shutdown -r` on some platforms in the past – warren Aug 23 '09 at 02:04
  • I don't think that we should suggest to execute `init 6` on an unknown path, just because a user of Solaris suggests that. I think Solaris is nice but the OP was asking about the Linux kernel. – Valerio Bozzolan Jul 05 '23 at 09:59
8

The easiest way I can think of is:

# sleep 2h && reboot

Run this as root.

Nick ODell
  • 194
  • 9
Omry
  • 685
  • 1
  • 7
  • 16
1
echo "reboot" | at -m 23:00       

....

snh_nl
  • 147
  • 2
  • 8