stop_tasks 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/bin/sh
  2. # We are trying to be nice.
  3. # TERM everybody. Give them some time to die.
  4. # KILL might make some filesystems non-unmountable,
  5. # so we'll do it in stop_storage instead.
  6. killcnt=30
  7. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  8. echo "<*> `date '+%Y-%m-%d %H:%M:%S'` Executing '$0 $*'"
  9. showps() {
  10. # sleep 1 ensures that xargs will have time to start up.
  11. # This makes pslist less prone to random jitter.
  12. pslist=`{ sleep 1; ps -A -o comm=; } | sort | xargs`
  13. pscnt=$(( `echo "$pslist" | wc -w` + 0 ))
  14. if test x"$VERBOSE" = x; then
  15. echo "* `date '+%H:%M:%S'` $pscnt processes"
  16. else
  17. echo "* `date '+%H:%M:%S'` Processes ($pscnt): $pslist"
  18. fi
  19. }
  20. # Sync.
  21. # Rationale: sometimes buggy root processes can
  22. # hang the system when killed (X for example may have problems
  23. # with restoring text mode on a poorly supported hardware).
  24. # These are bugs and must be fixed, but until then users will lose
  25. # dirty data on shutdown! Let's make that less likely.
  26. sync &
  27. # Send SIGTERMs. If list of processes changes, proceed slower.
  28. # If it has stabilised (all who wanted to, exited), proceed faster.
  29. showps
  30. i="$killcnt"
  31. while test "$i" -gt 0; do
  32. echo "* `date '+%H:%M:%S'` Sending CONT, TERM" #, HUP"
  33. # I've seen "killall5 2.86" which doesn't grok signal names!
  34. killall5 -18
  35. killall5 -15
  36. #killall5 -1 # HUP: because interactive bash does not die on TERM...
  37. # but init will reread /etc/inittab on HUP and my /etc is on non root fs!
  38. # -> umounts will complain.
  39. oldpslist="$pslist"
  40. showps
  41. if test x"$pslist" = x"$oldpslist"; then
  42. i=$((i-8))
  43. fi
  44. i=$((i-2))
  45. done
  46. echo "* `date '+%H:%M:%S'` Turning off swap"
  47. swapoff -a
  48. cat /proc/swaps | grep -v ^Filename | cut -d ' ' -f1 \
  49. | while read -r line; do
  50. test "$line" && {
  51. echo swapoff "$line"
  52. swapoff "$line"
  53. }
  54. done
  55. echo "* /proc/swaps:"
  56. cat /proc/swaps
  57. echo "* /proc/mounts:"
  58. cat /proc/mounts
  59. echo "* ps -A e:"
  60. ps -A e
  61. echo "* top -bn1:"
  62. top -bn1