shutdown.sh (1425B)
1 #!/bin/sh 2 # shutdown - shutdown(8) lookalike for fiss 3 4 abort() { 5 printf '%s\n' "$1" >&2 6 exit 1 7 } 8 9 usage() { 10 abort "Usage: ${0##*/} [-fF] [-kchPr] time [warning message]" 11 } 12 13 action=: 14 15 while getopts akrhPHfFnct: opt; do 16 case "$opt" in 17 a|n|H) abort "'-$opt' is not implemented";; 18 t) ;; 19 f) touch /fastboot;; 20 F) touch /forcefsck;; 21 k) action=true;; 22 c) action=cancel;; 23 h|P) action=halt;; 24 r) action=reboot;; 25 [?]) usage;; 26 esac 27 done 28 shift $((OPTIND - 1)) 29 30 [ $# -eq 0 ] && usage 31 32 time=$1; shift 33 message="${*:-system is going down}" 34 35 if [ "$action" = "cancel" ]; then 36 kill "$(cat /run/fiss/shutdown.pid)" 37 if [ -e /etc/nologin ] && ! [ -s /etc/nologin ]; then 38 rm /etc/nologin 39 fi 40 echo "${*:-shutdown cancelled}" | wall 41 exit 42 fi 43 44 touch /run/fiss/shutdown.pid 2>/dev/null || abort "Not enough permissions to execute ${0#*/}" 45 echo $$ >/run/fiss/shutdown.pid 46 47 case "$time" in 48 now) time=0;; 49 +*) time=${time#+};; 50 *:*) abort "absolute time is not implemented";; 51 *) abort "invalid time";; 52 esac 53 54 for break in 5 0; do 55 [ "$time" -gt "$break" ] || continue 56 [ "$break" = 0 ] && touch /etc/nologin 57 58 printf '%s in %s minutes\n' "$message" "$time" | wall 59 printf 'shutdown: sleeping for %s minutes... ' "$(( time - break ))" 60 sleep $(( (time - break) * 60 )) 61 time="$break" 62 printf '\n' 63 64 [ "$break" = 0 ] && rm /etc/nologin 65 done 66 67 printf '%s NOW\n' "$message" | wall 68 69 $action