Handy scripts you'd like to share

Enable and execute the Magic Sysrq key combo via a bash alias for a graceful shutdown during a system hang.

Run this command to create an /etc/sysctl.d/99-sysctl.conf file:

echo kernel.sysrq=1 | sudo tee -a /etc/sysctl.d/99-sysctl.conf

Then run the following command to reload the sysctl config files without rebooting the computer:

su -c "sysctl --system"

Create a script to simulate pressing the magic sysrq key combination required to shutdown the computer gracefully during a system hang.

Create the script /usr/local/bin/magic.sh:

#!/bin/sh
# cat /usr/local/bin/magic.sh

for c in r s u o; do
  echo $c > /proc/sysrq-trigger
  sleep 2
done

Make the script executable:

sudo chmod +x /usr/local/bin/magic.sh

Then, add the following line to the ~/.bashrc file:

alias magic='sudo /usr/local/bin/magic.sh'

Then do:

source ~/.bashrc

SysRQ r s u o accomplishes a graceful shutdown without using the more usual r e i s u b magic sysrq key combination. Removed e and i since they kill processes, including the current shell and the rest of the script would also be killed.

r s u o =

r:

Turns off keyboard raw mode and sets it to XLATE.

s:

Will attempt to sync all mounted filesystems.

u:

Will attempt to remount all mounted filesystems read-only.

o:

Will shut your system off, (if configured).


This is a slightly modified version of a script from this source:

automate the magic sysrq


At the terminal type magic to easily execute a graceful shutdown during a system hang.


11 Likes