24

I work on a ubuntu server located in a place where I don't usually go. I use ssh over openvpn to access it. The machine has multiple wired interfaces, many static routes and multiple default gateways to WAN. I frequently change interface adresses and routes. Most of the times the changes I do are temporarily--a reboot would reset the experiments I have done. In some cases some of those modifications have gone wrong, and the machine has gone offline. In such cases I had to call up someone at the place where the server is located to send the machine into a reboot.

Now I would like to schedule a reboot--lets say after 2 hours the machine should go into a reboot cycle. And in those two hours I can do whatever temporary experiments that I want. If all the temporary changes succeed then I should be able to prevent the "afer 2 hour" reboot from happening. So what I want is:

  1. How to tell the server to reboot after a certian time?
  2. If I decide that the machine should not go into a reboot, I should be able to cancel it.
nixnotwin
  • 4,993
  • 17
  • 53
  • 71

2 Answers2

41

You can also use at to schedule reboots. For example sudo at 22:00 to run the command at 22:00 as root. Then enter the command you want to run: /sbin/shutdown -r now and end with ctrl-d. If you decide not to run this command, run sudo atrm 1 to delete the first one in the at queue. With sudo atq you can see the queue.

Moshe Katz
  • 491
  • 1
  • 7
  • 21
kimda
  • 411
  • 1
  • 3
  • 3
  • This solution is perfect! – Sukima Jan 08 '13 at 15:08
  • 6
    This should be marked as the correct answer! The `at` family of commands is something every server admin should know. – Dave Apr 01 '13 at 18:41
  • 4
    in case you are new to `at`, first execute `sudo at 22:00`. This takes you into a kind of shell. Then you enter the command, and `ctrl-d` to exit. See also http://mixeduperic.com/ubuntu/how-to-schedule-a-one-time-restart-on-your-ubuntu-system-using-the-at-command.html – Tim Richardson Aug 22 '16 at 04:29
16

On the terminal type shutdown -r 10 Waiting 10 minutes before the restart& that will make the server reboot in 10 mins, broadcast a message explaining that and pressing control+c will interrupt the reboot request.

On the terminal type shutdown -r 18:00 Rebooting at 18:00 hours& that will make the server reboot at 18:00 hours, will broadcast a message explaining that and can be interrupted with control+c.

You can also interrupt any shutdown / reboot request on your "working" terminal with shutdown -a.

Bruno Pereira
  • 72,895
  • 33
  • 199
  • 223
  • 3
    This is a bit problematic since it is interactive and blocks the shell you are running. I'm not sure if this would work predictably over SSH if the session drops. – Alain O'Dea Dec 02 '12 at 23:30
  • I could not find any combination of `nohup` and `sudo` that would make this work if run from a normal ssh session as a normal admin group user. – Dave Apr 01 '13 at 18:03
  • Could this be run in combination with Screen to allow the command to be applied and the ssh session dropped? – Andy Davies Jan 29 '15 at 09:05