summary refs log tree commit diff
path: root/shutdown
diff options
context:
space:
mode:
Diffstat (limited to 'shutdown')
-rwxr-xr-xshutdown74
1 files changed, 74 insertions, 0 deletions
diff --git a/shutdown b/shutdown
new file mode 100755
index 0000000..4a3147b
--- /dev/null
+++ b/shutdown
@@ -0,0 +1,74 @@
+#!/bin/sh
+# shutdown - shutdown(8) lookalike for runit
+
+single() {
+  runsvchdir single
+}
+
+abort() {
+  echo "$@" >&2
+  exit 1
+}
+
+usage() {
+  abort "Usage: shutdown [-fF] [-kchPr] time [warning message]"
+}
+
+action=single
+
+while getopts akrhPHfFnct: opt; do
+  case "$opt" in
+    a|n|H) abort "-$opt is not implemented";;
+    t) ;;
+    f) touch /fastboot;;
+    F) touch /forcefsck;;
+    k) action=true;;
+    c) action=cancel;;
+    h|P) action=halt;;
+    r) action=reboot;;
+    [?]) usage;;
+  esac
+done
+shift $((OPTIND - 1))
+
+[ $# -eq 0 ] && usage
+
+time=$1; shift
+message="${*:-system is going down}"
+
+if [ "$action" = "cancel" ]; then
+  kill $(cat /run/runit/shutdown.pid)
+  echo "${*:-shutdown cancelled}" | wall
+  exit
+fi
+
+touch /run/runit/shutdown.pid 2>/dev/null || abort "Not enough permissions to execute ${0#*/}"
+echo $$ >/run/runit/shutdown.pid
+
+case "$time" in
+  now) time=0;;
+  +*) time=${time#+};;
+  *:*) abort "absolute time is not implemented";;
+  *) abort "invalid time";;
+esac
+
+if [ "$time" -gt 5 ]; then
+  echo "$message in $time minutes" | wall
+  printf "shutdown: sleeping for $time minutes... "
+  sleep $(((time - 5) * 60))
+  printf "\n"
+  time=5
+fi
+
+if [ "$time" -gt 0 ]; then
+  echo "$message in $time minutes" | wall
+  touch /etc/nologin
+  printf "shutdown: sleeping for $time minutes... "
+  sleep $((time * 60))
+  printf "\n"
+  rm /etc/nologin
+fi
+
+echo "$message NOW" | wall
+
+$action