busybox/examples/shutdown-1.0/script/shutdown
<<
>>
Prefs
   1#!/bin/sh
   2
   3PATH=/sbin:/usr/sbin:/bin:/usr/bin
   4
   5# Usually, /sbin/ has symlinks named halt, reboot, poweroff
   6# (and also possibly shutdown) to e.g.
   7# /app/shutdown-1.0/script/shutdown (this file).
   8cd /app/shutdown-1.0/script || exit 1
   9test -x ./do_shutdown || exit 1
  10test -x ./hardshutdown || exit 1
  11
  12# "reboot -f" -> "shutdown -f -r" -> "hardshutdown -r" -> immediate reboot
  13# "reboot" -> "shutdown -r" -> "do_shutdown -r"
  14# ^^^^^^^^^^^^^^^^^^ similarly for halt, poweroff.
  15# "shutdown" -> "do_shutdown" (everything killed/unmounted, but kernel not asked to do any poweroff etc)
  16force=""
  17test x"$1" = x"-f" && {
  18        force="-f"
  19        shift
  20}
  21test ! "$*" && test x"${0##*/}" = x"halt" && exec "$0" $force -h
  22test ! "$*" && test x"${0##*/}" = x"reboot" && exec "$0" $force -r
  23test ! "$*" && test x"${0##*/}" = x"poweroff" && exec "$0" $force -p
  24# We have something else than allowed parameters?
  25test x"$*" = x"" || test x"$*" = x"-h" || test x"$*" = x"-r" || test x"$*" = x"-p" || {
  26        echo "Syntax: $0 [-f] [-h/-r/-p]"
  27        exit 1
  28}
  29
  30# Emergency shutdown?
  31test "$force" && {
  32        exec ./hardshutdown "$@"
  33        exit 1
  34}
  35
  36# Normal shutdown
  37
  38# We must have these executables on root fs
  39# (mount/umount aren't checked, all systems are ok versus that):
  40test -x /bin/killall5 -o -x /sbin/killall5 || exit 1
  41test -x /bin/ps       -o -x /sbin/ps       || exit 1
  42test -x /bin/date     -o -x /sbin/date     || exit 1
  43test -x /bin/xargs    -o -x /sbin/xargs    || exit 1
  44test -x /bin/wc       -o -x /sbin/wc       || exit 1
  45test -x /bin/cat      -o -x /sbin/cat      || exit 1
  46test -x /bin/sort     -o -x /sbin/sort     || exit 1
  47
  48i="`ulimit -n`"
  49echo -n "Closing file descriptors $i-3... "
  50while test "$i" -ge 3; do
  51        eval "exec $i>&-"
  52        i=$((i-1))
  53done
  54
  55echo "Shutting down. Please stand by..."
  56
  57# setsid & /dev/null:
  58# make it a process leader & detach it from current tty.
  59# Why /dev/null and not /dev/console?
  60# I have seen a system which locked up while opening /dev/console
  61# due to the bug (?) in keyboard driver.
  62setsid env - PATH="$PATH" ./do_shutdown "$@" </dev/null >/dev/null 2>&1 &
  63
  64while true; do read junk; done
  65