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

Generated by cgit