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