shutdown 2.0 KB

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