What is /proc/sysrq-trigger in linux and how to use sysrq kernel feature ?

SysRq or “System Request” also known as Magic Keys, which allows us to send some specific instructions directly to the Linux kernel.

This can be invoked by using a specific keyboard sequence(according to the system hardware) or by echoing letter commands to /proc/sysrq-trigger.

We need to make sure that sysrq functionality is enabled in order to use this feature.

Lets have a look how to use these magical keys.

Enable SysRq

To enable sysrq, we need to echo 1 to /proc/sys/kernel/sysrq file.

[root@nglinux crash]# echo 1 > /proc/sys/kernel/sysrq 
[root@nglinux crash]# 

Following are the options we can echo to this file.
0 – disable sysrq completely
1 – enable all functions of sysrq
>1 – bitmask to allow specific sysrq functions
2 = 0x2 – enable control of console logging level
4 = 0x4 – enable control of keyboard (SAK, unraw)
8 = 0x8 – enable debugging dumps of processes etc.
16 = 0x10 – enable sync command
32 = 0x20 – enable remount read-only
64 = 0x40 – enable signalling of processes (term, kill, oom-kill)
128 = 0x80 – allow reboot/poweroff
256 = 0x100 – allow nicing of all RT tasks

 

Using Different sysrq magic keys

1. Available options

Jul 12 00:40:53 localhost kernel: SysRq : HELP : loglevel(0-9) reBoot Crash terminate-all-tasks(E) memory-full-oom-kill(F) kill-all-tasks(I) thaw-filesystems(J) saK show-backtrace-all-active-cpus(L) show-memory-usage(M) nice-all-RT-tasks(N) powerOff show-registers(P) show-all-timers(Q) unRaw Sync show-task-states(T) Unmount show-blocked-tasks(W) dump-ftrace-buffer(Z) 

2. Power off the machine immediately.

[root@nglinux ~]# echo o > /proc/sysrq-trigger 

3. Crash the system

[root@nglinux crash]# echo c > /proc/sysrq-trigger

4. Viewing Help: By echoing some random character which has no impact on system.

[root@nglinux ~]# echo y > /proc/sysrq-trigger 
[root@nglinux ~]# 

### View the messages file to see the output of the command.
[root@nglinux ~]# tail /var/log/messages
Jul 11 21:43:28 localhost NetworkManager[1565]:  (eth1): carrier now ON (device state 8)
Jul 11 21:43:42 localhost NetworkManager[1565]:  (eth1): carrier now OFF (device state 8, deferring action for 4 seconds)
Jul 11 21:43:42 localhost kernel: e1000: eth1 NIC Link is Down
Jul 11 21:43:46 localhost kernel: e1000: eth1 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
Jul 11 21:43:46 localhost NetworkManager[1565]:  (eth1): carrier now ON (device state 8)
Jul 11 21:44:40 localhost NetworkManager[1565]:  (eth1): carrier now OFF (device state 8, deferring action for 4 seconds)
Jul 11 21:44:40 localhost kernel: e1000: eth1 NIC Link is Down
Jul 11 21:44:42 localhost kernel: e1000: eth1 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
Jul 11 21:44:42 localhost NetworkManager[1565]:  (eth1): carrier now ON (device state 8)
Jul 11 21:47:12 localhost kernel: SysRq : HELP : loglevel(0-9) reBoot Crash terminate-all-tasks(E) memory-full-oom-kill(F) kill-all-tasks(I) thaw-filesystems(J) saK show-backtrace-all-active-cpus(L) show-memory-usage(M) nice-all-RT-tasks(N) powerOff show-registers(P) show-all-timers(Q) unRaw Sync show-task-states(T) Unmount show-blocked-tasks(W) dump-ftrace-buffer(Z) 

5. Sync all filesystems

[root@nglinux ~]# echo s > /proc/sysrq-trigger 
[root@nglinux ~]# 

6. Re-mounts all filesystems read only

[root@nglinux ~]# echo u > /proc/sysrq-trigger 

[root@nglinux ~]# touch abc
touch: cannot touch `abc': Read-only file system
[root@nglinux ~]#

7. Kill High memory processes to recover OOM situation

[root@nglinux ~]# echo f > /proc/sysrq-trigger

Jul 11 22:07:23 localhost kernel: Out of memory: Kill process 2037 (mysqld) score 34 or sacrifice child
Jul 11 22:07:23 localhost kernel: Killed process 2037, UID 27, (mysqld) total-vm:335736kB, anon-rss:26904kB, file-rss:3712kB

8. Kill all processes except init

[root@nglinux ~]# echo f > /proc/sysrq-trigger
[root@nglinux ~]# 

### Output:


9. See all blocked state processes on our system.

[root@nglinux ~]# echo w > /proc/sysrq-trigger 
[root@nglinux ~]# 

Jul 12 00:11:08 localhost kernel: SysRq : Show Blocked State
Jul 12 00:11:08 localhost kernel:  task                PC stack   pid father
Jul 12 00:11:08 localhost kernel: Sched Debug Version: v0.09, 2.6.32-754.el6.i686 #1
Jul 12 00:11:08 localhost kernel: now at 5075394.388115 msecs
Jul 12 00:11:08 localhost kernel:  .jiffies                                 : 4775394
Jul 12 00:11:08 localhost kernel:  .sysctl_sched_latency                    : 5.000000
Jul 12 00:11:08 localhost kernel:  .sysctl_sched_min_granularity            : 1.000000
Jul 12 00:11:08 localhost kernel:  .sysctl_sched_wakeup_granularity         : 1.000000
Jul 12 00:11:08 localhost kernel:  .sysctl_sched_child_runs_first           : 0.000000
Jul 12 00:11:08 localhost kernel:  .sysctl_sched_features                   : 7279
Jul 12 00:11:08 localhost kernel:  .sysctl_sched_tunable_scaling            : 1 (logaritmic)
Jul 12 00:11:08 localhost kernel: 
Jul 12 00:11:08 localhost kernel: cpu#0, 2693.764 MHz
Jul 12 00:11:08 localhost kernel:  .nr_running                    : 2
Jul 12 00:11:08 localhost kernel:  .load                          : 2048
Jul 12 00:11:08 localhost kernel:  .nr_switches                   : 244106
Jul 12 00:11:08 localhost kernel:  .nr_load_updates               : 50168
Jul 12 00:11:08 localhost kernel:  .nr_uninterruptible            : 0
Jul 12 00:11:08 localhost kernel:  .next_balance                  : 4.775427
Jul 12 00:11:08 localhost kernel:  .curr->pid                     : 3898
Jul 12 00:11:08 localhost kernel:  .clock                         : 9044243.494162
Jul 12 00:11:08 localhost kernel:  .cpu_load[0]                   : 0
Jul 12 00:11:08 localhost kernel:  .cpu_load[1]                   : 0
Jul 12 00:11:08 localhost kernel:  .cpu_load[2]                   : 0
Jul 12 00:11:08 localhost kernel:  .cpu_load[3]                   : 0
Jul 12 00:11:08 localhost kernel:  .cpu_load[4]                   : 0
Jul 12 00:11:08 localhost kernel:  .yld_count                     : 3
Jul 12 00:11:08 localhost kernel:  .sched_switch                  : 0
Jul 12 00:11:08 localhost kernel:  .sched_count                   : 245424
Jul 12 00:11:08 localhost kernel:  .sched_goidle                  : 51895
Jul 12 00:11:08 localhost kernel:  .avg_idle                      : 979791
Jul 12 00:11:08 localhost kernel:  .ttwu_count                    : 176590
Jul 12 00:11:08 localhost kernel:  .ttwu_local                    : 176590
Jul 12 00:11:08 localhost kernel:  .bkl_count                     : 324
Jul 12 00:11:08 localhost kernel: 
Jul 12 00:11:08 localhost kernel: cfs_rq[0]:/
Jul 12 00:11:08 localhost kernel:  .exec_clock                    : 25245.714291
Jul 12 00:11:08 localhost kernel:  .MIN_vruntime                  : 22133.515042
Jul 12 00:11:08 localhost kernel:  .min_vruntime                  : 22133.515042
Jul 12 00:11:08 localhost kernel:  .max_vruntime                  : 22133.515042
Jul 12 00:11:08 localhost kernel:  .spread                        : 0.000000
Jul 12 00:11:08 localhost kernel:  .spread0                       : 0.000000
Jul 12 00:11:08 localhost kernel:  .nr_spread_over                : 3735
Jul 12 00:11:08 localhost kernel:  .nr_running                    : 2
Jul 12 00:11:08 localhost kernel:  .load                          : 2048
Jul 12 00:11:08 localhost kernel:  .throttled                     : 0
Jul 12 00:11:08 localhost kernel:  .throttle_count                : 0
Jul 12 00:11:08 localhost kernel:  .load_avg                      : 0.000000
Jul 12 00:11:08 localhost kernel:  .load_period                   : 0.000000
Jul 12 00:11:08 localhost kernel:  .load_contrib                  : 0
Jul 12 00:11:08 localhost kernel:  .load_tg                       : 0
Jul 12 00:11:08 localhost kernel: 
Jul 12 00:11:08 localhost kernel: rt_rq[0]:/
Jul 12 00:11:08 localhost kernel:  .rt_nr_running                 : 0
Jul 12 00:11:08 localhost kernel:  .rt_throttled                  : 0
Jul 12 00:11:08 localhost kernel:  .rt_time                       : 0.000000
Jul 12 00:11:08 localhost kernel:  .rt_runtime                    : 950.000000
Jul 12 00:11:08 localhost kernel: 
Jul 12 00:11:08 localhost kernel: runnable tasks:
Jul 12 00:11:08 localhost kernel:            task   PID         tree-key  switches  prio     exec-runtime         sum-exec        sum-sleep
Jul 12 00:11:08 localhost kernel: ----------------------------------------------------------------------------------------------------------
Jul 12 00:11:08 localhost kernel:        events/0     7     22133.515042     10961   120     22133.515042      1599.376275   5073538.885707 /
Jul 12 00:11:08 localhost kernel: R           bash  3898     22131.188707        76   120     22131.188707        45.901986     99140.785267 /
Jul 12 00:11:08 localhost kernel: 

5 8 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments