29

Assuming you've just entered the command shutdown -h now (or something equivalent), is there realistically anything you can do (if you're quick enough) to prevent the machine shutting down?

Jonathan
  • 391
  • Once you invoke shutdown -h now, I believe you lose command line access immediately similarly to halt -p so there isn't much you can do. – Ken Jan 18 '11 at 16:41
  • @Ken: on Ubuntu and Centos connected via putty the command line remains available for a few seconds. –  Jan 18 '11 at 18:43
  • Why say "now" if you don't mean "right now"? – Fred Nurk Jan 18 '11 at 20:27

5 Answers5

26

You can run shutdown -c to cancel an already running shutdown.

dogbane
  • 29,677
  • 1
    Did not believe it at first, but yes. More specifically ctrl+c – Ken Jan 18 '11 at 16:45
  • 4
    @Ken: shutdown -c and [Ctrl]+[c] are entirely different. shutdown -c is a command which cancels a previously scheduled shutdown (even "now" is scheduled in this sense --- but whether the cancellation will prevent the reboot is a bit of a race in that case). [Ctrl]+[c] will cause you terminal to generate a SIGINT (interrupt signal) to whichever process was currently attached to it. This might work on a running shutdown command but that's rather incidental to your question. – Jim Dennis Jan 18 '11 at 17:25
  • 1
    @Jim Dennis: I'm aware of the difference between ctrc+c and shutdown -c. But given the speed of the -now option rather than its scheduled sibling, ctrl+c would be the fastest route to that end. I didn't believe you could stop an immanent shutdown, even more surprised that you can interrupt it, but apparently you can, but it's a race as you stated. – Ken Jan 18 '11 at 17:45
16

If you are fast enough you can issue an init 2 (or whatever runlevel you want) and that will likely stop the shutdown. Anything involving killing the shutdown command will fail as the command runs too quickly

I tried this with the script below and and even it's not fast enough to stop the shutdown

#!/bin/bash
shutdown -h now
shutdown -c "Aborting Shutdown"

Broadcast message from iain@iain-ubuntu
        (/dev/pts/0) at 16:50 ...

The system is going down for halt NOW!
shutdown: Cannot find pid of running shutdown

Where as interactively

shutdown -h now
init 2 

the system stays up.

3

From the FreeBSD man page:

A scheduled shutdown can be canceled by killing the shutdown process (a SIGTERM should suffice).

So killall shutdown works on BSD when there is a scheduled shutdown (e.g. shudown -h +5).

I don't think it's feasible to cancel an immediate shutdown (shutdown -h now) in real-life scenario.

0

suppose, you sent a reboot or shutdown message to all terminal for maintenance purpose that the "server is going to reboot or shutdown for maintenance purpose within 10 minutes"

This kind of command can cancel.

just press Ctrl+C

HalosGhost
  • 4,790
Manjur
  • 9
0

I just did an accidental

shutdown -h

i.e. without the now

In a moment of "underthinking" I was thinking I would like to see the usage for the command and so invoked the standard "-h" for help ---- noooooo not for shutdown!!

I wasn't aware of the -c option to cancel the shutdown but in a panic a issued a very quick

shutdown -r now

which seems to have overridden the previously issued 'halt' and replaced it with a 'reboot'.

The machine rebooted instead of halted - luckily!