File: AutoHibernate

package info (click to toggle)
fvwm-crystal 3.4.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 21,700 kB
  • ctags: 1,494
  • sloc: sh: 3,265; cs: 1,335; python: 875; makefile: 214
file content (54 lines) | stat: -rwxr-xr-x 1,523 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
# FVWM-Crystal helper script that put the omputer in hibernation
# when the battery is low
# Usage: Exec exec $[FVWM_SYSTEMDIR]/scripts/AutoHibernate <rate [%]>

# the PID to kill if fvwm is interrupted
TMPFILE="/tmp/crystal_autohibernate_$$"
touch ${TMPFILE}

cleanup() {
	rm ${TMPFILE}
	exit 0
}

trap cleanup INT QUIT TERM

if [[ ! "$1" ]]; then
	cleanup
fi

# write the preference file
echo "Exec exec ${FVWM_SYSTEMDIR}/scripts/AutoHibernate $1" > ${FVWM_USERDIR}/preferences/AutoHibernation

# the main loop
CurrentCharge=$(($(cat /sys/class/power_supply/BAT0/energy_now)/(($(cat /sys/class/power_supply/BAT0/energy_full)/100))))
MinCharge=$(($1+1))
Resumed="0"

while :
do
	sleep "5"
	# fvwm is started by exec; be sure it is running
	pidof fvwm 1>/dev/null || cleanup

	if [[ "$CurrentCharge" -lt "$MinCharge" ]]; then
		# avoid hibernation loop
		if [[ "${Resumed}" == "0" ]] ; then
			# mplayer doesn't like hibernation
			${FVWM_SYSTEMDIR}/scripts/killmplayer 9
			killall -9 mplayer 2>/dev/null
			sudo pm-hibernate
			# we must kill that script after rebbot to avoid loop
			Resumed="1"
		else
			CurrentCharge=$(($(cat /sys/class/power_supply/BAT0/energy_now)/(($(cat /sys/class/power_supply/BAT0/energy_full)/100))))
		fi
	else
		CurrentCharge=$(($(cat /sys/class/power_supply/BAT0/energy_now)/(($(cat /sys/class/power_supply/BAT0/energy_full)/100))))
		# return to normal loop after resuming when CurrentCharge > $1
		if [[ "$CurrentCharge" -gt "$MinCharge" ]]; then
			Resumed="0"
		fi
	fi
done