#!/usr/bin/bash
# sysrq.sh
# direct kernel control of reboot, poweroff, etc.
# does not depend on systemd or init.d
# all output goes to dmesg & /var/log/messages
#
# see docs at:
# /usr/share/doc/kernel-doc/Documentation/admin-guide/sysrq.rst
#
# SLOS kernels are already enabled for all sysrq requests
#    echo 1 > /proc/sys/kernel/sysrq
#
# help:
# [7153567.545738] sysrq: SysRq : HELP : loglevel(0-9) reboot(b) crash(c) terminate-all-tasks(e) memory-full-oom-kill(f) kill-all-tasks(i) thaw-filesystems(j) sak(k) show-backtrace-all-active-cpus(l) show-memory-usage(m) nice-all-RT-tasks(n) poweroff(o) show-registers(p) show-all-timers(q) unraw(r) sync(s) show-task-states(t) unmount(u) show-blocked-tasks(w) dump-ftrace-buffer(z) 
#
# Questions to John Masinter

# Keep version in sync with RPM version
VER="1.0-1"

# must be root
[ $(id -u) != 0 ] && echo "$(basename $0): Must be root." && exit 1

#----------------------------------------------------------------------
# display menu
#----------------------------------------------------------------------
Menu() {
   echo -n '
-----------------------------------
- Direct Kernel Control via sysrq -
-----------------------------------

See /var/log/messages for all output
Version '$VER'

x  - Quit this program
b  - Reboot immediately (no sync)
c  - Crash kernel
o  - Power Off
s  - Sync disk
r  - Fix console (unset RAW mode)
u  - Remount all filesystems R/O
h  - Print help
0-9  Set log level 0 lowest, 9 highest

Selection: '

} # Menu

#----------------------------------------------------------------------
# loop reading selections and executing
Run() {
   while : ; do
      Menu
      read A
      if [ ${#A} -ne 1 ] ; then
         echo
         echo "*** Please enter selection as one character. (Enter to continue)"
         read B
      elif [ "$A" == "x" ] || [ "$A" == "q" ] ; then
         echo
         echo "Quit..."
         exit 0
      else 
         echo
         echo "echo $A > /proc/sysrq-trigger"
         echo $A > /proc/sysrq-trigger
         sleep 2
      fi
   done
} # Run

#----------------------------------------------------------------------
# Main
Run

