2

Sometimes I would like to shut down in a hurry. By hurry I mean 5 to 10 seconds. Assume I can run any script with root privileges in tty which I've prepared beforehand, and after 5 or 10 seconds the power will go out. What's the best way to minimize the damage (like maybe unmounting all devices, or closing all open files)?

ike
  • 1,210
  • 1
  • 14
  • 29

2 Answers2

4

Anyway you can shutdown from terminal using the command,

sudo shutdown -h now

or,

sudo poweroff

Afterwards you need to put your sudo password. Here is an answer describes how you can shutdown from terminal without entering password. To do so,

Just run sudo visudo from terminal and add the following line:

%user_name ALL=(ALL) NOPASSWD: /sbin/poweroff, /sbin/shutdown

This allows you to run the above commands, using sudo, however with no password. So, sudo poweroff or sudo shutdown -h now will now result in a password less shutdown.

If you want a step further ahead you can make a alias named stopnow or whatever,

alias stopnow='sudo poweroff'

That would enable you to shutdown the system by only running stopnow in terminal.

sourav c.
  • 44,715
  • sudo init 0 may be a faster command, but would skip the notifying users and wont stop applications that are running, which may be bad. – Paul Tanzini Dec 12 '13 at 15:05
  • 1
    This command won't do anything anyway without a time: shutdown: time expected And to shutdown you need the -h option. And this can sometimes take a long time which I don't always have. – ike Dec 12 '13 at 15:10
  • @Dash_plus_Java you are correct. – sourav c. Dec 12 '13 at 15:24
  • 2
    As in the linked answer: sudo poweroff should be quite fast and safe. I use it a lot when testing @#$% kernel boot parameters... – Rmano Dec 12 '13 at 16:46
  • @ike Perhaps you missed the hyperlink. Plz follow it. I edited my answer to make more sense. There was the hyperlink to an answer how to shutdown from terminal without entering password – sourav c. Dec 12 '13 at 17:02
  • @souravc shutdown alone was working in the linked answer because it was (unwisely) aliased to sudo shutdown now. You can alias STOPNOW to sudo poweroff still... – Rmano Dec 12 '13 at 17:03
  • @Rmano you are correct. Actually I tried to pointed out the first part how to edit visudo. I am editing my answer. – sourav c. Dec 12 '13 at 17:07
  • @Rmano sudo poweroff took about 25 seconds on my machine. – ike Dec 12 '13 at 17:11
  • 1
    @ike Yikes! On my laptop is more around 10 seconds. Have you tried the sudo init 0 suggestion? It is not clean but cleaner than the Magic SysRQ. See also http://superuser.com/questions/108704/how-to-shutdown-instantly – Rmano Dec 12 '13 at 17:15
  • @Rmano Why are you suggesting sudo init 0? I think it is not completely safe. – sourav c. Dec 12 '13 at 17:29
  • 1
    No, it's not safe --- nothing short of the correct shutdown is safe. Maybe the OP should try to see why poweroff is so slow? – Rmano Dec 12 '13 at 17:32
  • @Rmano Do you want some log files? I think it took about 15 seconds to get to waiting for all processes to terminate, then about 10 to power off. And I have a Dell XPS 8300 with 16GB ram and core i7 2nd gen and ubuntu 13.10 – ike Dec 12 '13 at 17:34
  • I have no time now to check --- I half remember there was a too around to profile startup and shutdown for that (could be a good question to post, hint). Try to see if you can pin down the process that are delaying the poweroff. – Rmano Dec 12 '13 at 17:38
  • sudo init 0 also took about 25 seconds – ike Dec 12 '13 at 20:03
3

Alt+Print Screen+R,E,I,S,U. Then Power Button to shut down.

Will quickly and safely shut down a computer running Linux (Ubuntu). It is talking directly to the kernel, and can be used at anytime.

Reference: http://en.wikipedia.org/wiki/Magic_SysRq_key

This is a modified version of REISUB. Since we don't want a restart, B is replaced with just shutting the computer down.

Paul Tanzini
  • 3,927
  • 2
    This will restart, not poweroff. – jobin Dec 12 '13 at 14:43
  • ok, so I guess replace B with O then to shut down? I'm not going to try it because I don't need to, but the Wikipedia article says that will shut it down. – Paul Tanzini Dec 12 '13 at 14:46
  • This is in short "Reboot Even If System Utterly Broken". It is not going to shutdown the system. – sourav c. Dec 12 '13 at 14:52
  • This looks pretty good, I'm going to test echo reisuo > /proc/sysrq-trigger now, see you in a few minutes if it works. – ike Dec 12 '13 at 14:52
  • The terminal command as root didn't do anything, the button combo did something, but not shutdown, more like a freeze, but on reboot, no fsck needed – ike Dec 12 '13 at 15:11
  • 2
    I don't think this is minimizing the damage, on the contrary, this should only be used when the system is not responding. Killing processes is never good. And even when using this key combo we should wait a few secs between pressing those keys. (let the processes which can terminate safely before we SIGKILL them, wait for the sync before we unmount, etc.) – falconer Dec 12 '13 at 15:13
  • Did you use REISUB or REISUO when you tried it? – Paul Tanzini Dec 12 '13 at 15:13
  • @Dash_plus_Java I tried REISUO – ike Dec 12 '13 at 15:34
  • Interesting; I know from experience using REISUB that as soon as you start it kills the screen and goes into a zombie like state. It doesn't actually power down until you hit that B. I'm not sure how the O combination works, but B is instant restart. If you can stop the power-on by hitting the power button, REISUB should work out for you. – Paul Tanzini Dec 12 '13 at 15:39
  • @Dash_plus_Java Unless you hit B it would not going to reboot. Every letter in the sequence do a separate job, like R=take control of keyboard back from X, E=send SIGTERM to all processes, allowing them to terminate gracefully, I=send SIGKILL to all processes, forcing them to terminate immediately, S=flush data to disk, U=remount all filesystems read-only. See wiki link. – sourav c. Dec 12 '13 at 15:45
  • 1
    @Dash_plus_Java why don't you edit your answer, it is not actually an answer to the question. isn't it? – sourav c. Dec 12 '13 at 15:46
  • @souravc Done. Since we don't need a reboot, just leave the B off and shut down the computer with power button. – Paul Tanzini Dec 12 '13 at 16:10