summaryrefslogtreecommitdiff
path: root/posts/linux:manual_restart_with_sysrq-trigger.adoc
blob: 38a54ebdc03695bb0b84efd688f9276547453f11 (plain)
    1 Linux:Manual Restart with sysrq-trigger
    2 =======================================
    3 :author: Aaron Ball
    4 :email: nullspoon@oper.io
    5 :revdate: February 15, 2016
    6 
    7 
    8 
    9 A little over a week ago I decided it would be fun to try to write my own
   10 multi-threaded init system in c. Without going into too much detail (I'll write
   11 a blog post that introduces it once it's further along), I needed to find a way
   12 to shutdown and restart my system from c (init 0 and 6, respectively).
   13 
   14 I looked around the internet and had a surprisingly difficult time finding what
   15 I was looking for. Every post I found indicated I should use the system
   16 function to call "shutdown -h -P now". While not a wrong or a bad answer, it
   17 wasn't the answer I was looking for. I wanted to know how the shutdown and
   18 reboot commands worked, or how my current init system executed runlevels 0 and
   19 6, respectively.
   20 
   21 Remembering something I found a ways back about kernel triggers found in and
   22 /sys/power/ for controlling acpi states, I started searching for something
   23 similar. After a bit of time, I found */proc/sysrq-trigger*. For further
   24 reading beyond this post, please see the
   25 https://en.wikipedia.org/wiki/Magic_SysRq_key[Wikipedia Article].
   26 
   27 
   28 
   29 Simple Usage
   30 ------------
   31 
   32 The usage of sysrq-trigger is fairly simple. Before we get into exact usage for
   33 the purposes of shutting down or rebooting though, please note that output from
   34 /proc/sysrq-trigger can only be seen from an actual tty, and not from within a
   35 terminal emulator.
   36 
   37 That said, execute the following command to find out all of the options
   38 available to you from sysrq.
   39 
   40     # echo h > /proc/sysrq-trigger
   41 
   42 If you don't see output from that, either sysrq isn't enabled, or you aren't
   43 executing from within a tty.
   44 
   45 To check if it is enabled, ensure that the following command yields a *1*.
   46 
   47     # cat /proc/sys/kernel/sysrq
   48 
   49 
   50 
   51 Reboot and Shutdown Sequences
   52 -----------------------------
   53 
   54 If you executed the _echo h_ statement above, you likely saw the output of
   55 supported commands. Among them you should havE seen *b* and *o*, for reboot and
   56 poweroff, respectively. _Note that it is a bad idea to execute either of these
   57 right now._
   58 
   59 If you read around the internet for what these two do, you'll see that both
   60 cause a hard stop. Neither polietly asks processes to exit. They are roughly
   61 equivelant to the hardware reset and power buttons. If rebooting or powering
   62 off, *we want b or o to be the last command sent* to have as graceful a stop as
   63 possible.
   64 
   65 
   66 Here is the list of commands that should be sent to sysrq-trigger, in order,
   67 for both a shutdown and a reboot.
   68 
   69 echo r > /proc/sysrq-trigger:: (un*R*aw) Takes back control of keyboard from X
   70 
   71 echo e > /proc/sysrq-trigger:: (t*E*rminate) Send SIGTERM to all processes. If
   72                                you aren't familiar with process signals, this
   73                                is basically a friendly kill command for every
   74                                process.
   75 
   76 echo i > /proc/sysrq-trigger:: (k*I*ll) Send SIGKILL to all processes. If you
   77                                aren't familiar with process signals, this is
   78                                basically a kill -9 command for every remaining
   79                                process.
   80 
   81 echo s > /proc/sysrq-trigger:: (*S*nc) Sync all cached disk operations to disk
   82 
   83 echo u > /proc/sysrq-trigger:: (*U*mount) Umounts all mounted partitions
   84 
   85 
   86 The previous commands all get us to a ready to shutdown or reboot state. To
   87 shutdown or reboot, execute one of the following.
   88 
   89 echo o > /proc/sysrq-trigger:: (p*O*weroff) Powers off the system
   90 
   91 echo b > /proc/sysrq-trigger:: (re*B*oot) Reboots the system
   92 
   93 
   94 The entire list in order, using the reboot command...
   95 
   96     # echo r > /proc/sysrq-trigger
   97     # echo e > /proc/sysrq-trigger
   98     # echo i > /proc/sysrq-trigger
   99     # echo s > /proc/sysrq-trigger
  100     # echo u > /proc/sysrq-trigger
  101     # echo b > /proc/sysrq-trigger
  102 
  103 
  104 [role="datelastedit"]
  105 Last edited: {revdate}
  106 // vim: set syntax=asciidoc:

Generated by cgit