unix/fiss

src/halt/shutdown.sh in master
Repositories | Summary | Log | Files | LICENSE

shutdown.sh (1425B) download


 1#!/bin/sh
 2# shutdown - shutdown(8) lookalike for fiss
 3
 4abort() {
 5  printf '%s\n' "$1" >&2
 6  exit 1
 7}
 8
 9usage() {
10  abort "Usage: ${0##*/} [-fF] [-kchPr] time [warning message]"
11}
12
13action=:
14
15while 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
27done
28shift $((OPTIND - 1))
29
30[ $# -eq 0 ] && usage
31
32time=$1; shift
33message="${*:-system is going down}"
34
35if [ "$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
42fi
43
44touch /run/fiss/shutdown.pid 2>/dev/null || abort "Not enough permissions to execute ${0#*/}"
45echo $$ >/run/fiss/shutdown.pid
46
47case "$time" in
48  now) time=0;;
49  +*) time=${time#+};;
50  *:*) abort "absolute time is not implemented";;
51  *) abort "invalid time";;
52esac
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
65done
66
67printf '%s NOW\n' "$message" | wall
68
69$action