bash-battery-suspend.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/bin/bash
  2. # MIT License
  3. # https://github.com/vladimirok5959/bash-battery-suspend
  4. # Settings
  5. BAT_SUSPEND_MSG_PERC="20"
  6. BAT_SUSPEND_ACT_PERC="15"
  7. # Consts
  8. CONST_PID_FILE="/tmp/batt.suspend.pid"
  9. batt_check_util() {
  10. util_name="$1"
  11. check=`\
  12. whereis $util_name | \
  13. awk '{ print $2 }' \
  14. `
  15. if [ "$check" = "" ]; then
  16. echo "Error: '$util_name' is not found..."
  17. exit
  18. fi
  19. }
  20. batt_get_percentage() {
  21. percentage=`\
  22. upower \
  23. -i /org/freedesktop/UPower/devices/battery_BAT0 | \
  24. grep "percentage:" | \
  25. awk '{ print $2 }' | \
  26. sed 's/[^0-9]$//g' \
  27. `
  28. echo "${percentage}"
  29. }
  30. batt_warning_msg() {
  31. notify-send \
  32. --urgency=critical \
  33. "Battery is critically low" \
  34. "OS will be suspended on ${BAT_SUSPEND_ACT_PERC}%"
  35. }
  36. batt_suspend() {
  37. systemctl suspend -i
  38. }
  39. batt_pid_get() {
  40. if [ -f "${CONST_PID_FILE}" ]; then
  41. cat ${CONST_PID_FILE}
  42. else
  43. echo "0"
  44. fi
  45. }
  46. batt_pid_set() {
  47. echo "$1" > ${CONST_PID_FILE}
  48. }
  49. batt_pid_del() {
  50. if [ -f "${CONST_PID_FILE}" ]; then
  51. rm ${CONST_PID_FILE}
  52. fi
  53. }
  54. batt_loop() {
  55. BAT_DO_MSG="0"
  56. BAT_DO_ACT="0"
  57. while [ 1 ]
  58. do
  59. current=$(batt_get_percentage)
  60. if (( BAT_DO_MSG == 0 )); then
  61. if (( current <= BAT_SUSPEND_MSG_PERC )); then
  62. BAT_DO_MSG="1"
  63. batt_warning_msg
  64. fi
  65. fi
  66. if (( current <= BAT_SUSPEND_ACT_PERC )); then
  67. BAT_DO_ACT="1"
  68. break
  69. fi
  70. sleep 2
  71. done
  72. batt_pid_del
  73. if (( BAT_DO_ACT == 1 )); then
  74. batt_suspend
  75. fi
  76. }
  77. start() {
  78. pid=$(batt_pid_get)
  79. if (( pid == 0 )); then
  80. (
  81. batt_loop
  82. exit 0
  83. )&
  84. batt_pid_set "$!"
  85. status
  86. else
  87. echo "Already!"
  88. status
  89. fi
  90. }
  91. stop() {
  92. pid=$(batt_pid_get)
  93. if (( pid == 0 )); then
  94. echo "Script is not runned"
  95. else
  96. kill -9 ${pid}
  97. batt_pid_del
  98. echo "Stopped!"
  99. fi
  100. }
  101. status() {
  102. pid=$(batt_pid_get)
  103. if (( pid == 0 )); then
  104. echo "Status: is not runned"
  105. else
  106. echo "Status: runned"
  107. echo "Alert perc: ${BAT_SUSPEND_MSG_PERC}%"
  108. echo "Suspend perc: ${BAT_SUSPEND_ACT_PERC}%"
  109. fi
  110. }
  111. usage() {
  112. echo "$0 (start|stop|status)"
  113. status
  114. }
  115. batt_check_util "grep"
  116. batt_check_util "upower"
  117. batt_check_util "systemctl"
  118. batt_check_util "notify-send"
  119. case $1 in
  120. "start")
  121. start
  122. ;;
  123. "stop")
  124. stop
  125. ;;
  126. "status")
  127. status
  128. ;;
  129. *)
  130. usage
  131. ;;
  132. esac
  133. exit