do_shutdown 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/bin/sh
  2. # We are called with stdin/out/err = /dev/null
  3. resetgracetime=60
  4. logfile="/var/log/reboot/`date '+%Y%m%d%H%M%S'`.log"
  5. mkdir -p /var/log/reboot
  6. PATH=/sbin:/bin
  7. say() {
  8. printf "\r%s\n\r" "$*"
  9. }
  10. # Since there is a potential for various fuckups during umount,
  11. # we start delayed hard reboot here which will forcibly
  12. # reboot hung box in a remote datacenter a thousand miles away ;)
  13. if test "$1" = "-r"; then
  14. ./hardshutdown -r "$resetgracetime" &
  15. fi
  16. # Now, (try to) switch away from X and open a console. I've seen reboots
  17. # hung on open("/dev/console"), therefore we do it _after_ hardshutdown
  18. exec >/dev/console 2>&1
  19. if test "$1" = "-r"; then
  20. say "* `date '+%H:%M:%S'` Scheduled hard reboot in $resetgracetime seconds"
  21. fi
  22. say "* `date '+%H:%M:%S'` Stopping tasks (see /var/log/reboot/* files)"
  23. # log reboot event to file. %Y%m%d%H%M%S: YYYYMMDDHHMMSS
  24. ./stop_tasks >"$logfile" 2>&1
  25. # Dying X tends to leave us at semi-random vt. Try to fix that,
  26. # but if it doesn't work, proceed anyway.
  27. exec >/dev/null 2>&1
  28. chvt 1 & sleep 1
  29. exec >/dev/console 2>&1
  30. command -v ctrlaltdel >/dev/null && {
  31. say "* `date '+%H:%M:%S'` Setting Ctrl-Alt-Del to 'hard'"
  32. ctrlaltdel hard
  33. }
  34. say "* `date '+%H:%M:%S'` Stopping storage devices"
  35. # we can't log this: we are about to unmount everything!
  36. ./stop_storage "$@"
  37. # If we have cmdline params, start hardshutdown with them
  38. test "$*" && ./hardshutdown "$@"
  39. # Just sleep endlessly...
  40. say "* `date '+%H:%M:%S'` You may now power off or press Ctrl-Alt-Del to reboot"
  41. while true; do sleep 32000; done