powerdown 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/bin/sh
  2. # Uncomment the following line to get debug info
  3. #set -x
  4. SELF="$(basename ${0})"
  5. REBOOTING_FILE="/run/rebooting"
  6. usage() {
  7. >&2 echo "Usage: ${SELF} schedule delay"
  8. >&2 echo " ${SELF} handle"
  9. >&2 echo " ${SELF} now"
  10. exit 1
  11. }
  12. schedule_powerdown() {
  13. # Send USR1 signal to the running FunKey process to warn about
  14. # impending shutdown
  15. kill -USR1 "$(pid print)" > /dev/null 2>&1
  16. # Delay for the given grace period seconds to catch signal USR2.
  17. # If the signal is caught, then it means the running FunKey
  18. # process canceled this shutdown and will handle it by itself.
  19. sleep ${1}
  20. # Delay expired, initiate final powerdown
  21. powerdown_now
  22. }
  23. handle_powerdown() {
  24. pkill -f "powerdown schedule"
  25. }
  26. powerdown_now() {
  27. # Sync before all else
  28. sync
  29. # Notif fullscreen "Shutting down"
  30. notif set 0 "^^^^^^^^ SHUTTING DOWN...^^^^^^^^"
  31. # Notify system, reboot in progress
  32. touch "${REBOOTING_FILE}"
  33. # Shutdown amp
  34. audio_amp off >/dev/null 2>&1
  35. # Force Read Only
  36. ro
  37. # Poweroff
  38. poweroff
  39. }
  40. action="${1:-now}"
  41. case "${action}" in
  42. schedule)
  43. if [ ${#} != 2 -o "${2}" == "0" ]; then
  44. usage
  45. fi
  46. schedule_powerdown ${2}
  47. ;;
  48. handle)
  49. if [ ${#} -ne 1 ]; then
  50. usage
  51. fi
  52. handle_powerdown
  53. ;;
  54. now)
  55. if [ ${#} -gt 1 ]; then
  56. usage
  57. fi
  58. powerdown_now
  59. ;;
  60. *)
  61. usage
  62. ;;
  63. esac
  64. exit 0