powerdown 1.4 KB

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