Skip to content

Instantly share code, notes, and snippets.

@mozbugbox
Created August 10, 2014 13:08
Show Gist options
  • Save mozbugbox/f633192ceb2ee4526494 to your computer and use it in GitHub Desktop.
Save mozbugbox/f633192ceb2ee4526494 to your computer and use it in GitHub Desktop.
Auto suspend Linux box with xscreensaver
#!/bin/bash
# suspend/sleep linux system on xscreensaver activation
#
# run the bash script in your .xsession,
# or something like .config/lxsession/LXDE/autostart for lxde
timeout=60 # After xscreensaver activated, wait timeout to suspend
read_timeout=5 # timeout for read from `xscreensaver-command -watch`
SUSPEND_BIN=/usr/sbin/pm-suspend
XSCREEN_CMD=/usr/bin/xscreensaver-command
timeout=$(($timeout/$read_timeout))
blanked=0
timer=0
#echo $timeout
# count down before suspend
do_suspend() {
timer=$(($timer + 1))
if [ ${timer} -ge ${timeout} ]; then
timer=0
sudo ${SUSPEND_BIN}
#echo "do suspend"
fi
#echo "timer: ${timer}"
}
process_watch() {
# suspend after screen blank
while :
do
# read xscreensaver -watch output
read -t ${read_timeout} input
if [[ $? -gt 0 && $? -lt 128 ]]; then
break
fi
case "$input" in
UNBLANK*)
blanked=0
timer=0
;;
LOCK* | BLANK*)
if [ ${blanked} = 0 ]; then
blanked=1
timer=0 # restart timer
fi
;;
esac
#echo "$input $blanked"
# we are blanked, check do_suspend
if [ ${blanked} -gt 0 ]; then
do_suspend
fi
done
}
${XSCREEN_CMD} -watch | process_watch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment