Skip to content

Instantly share code, notes, and snippets.

@igal1c0de4n
Last active August 29, 2015 14:06
Show Gist options
  • Save igal1c0de4n/0e0e39815ec7a0fa9b25 to your computer and use it in GitHub Desktop.
Save igal1c0de4n/0e0e39815ec7a0fa9b25 to your computer and use it in GitHub Desktop.
Are you running Linux with Gnome-Shell, installed packages and auto-suspend is broken? You can use this bash script to force suspend when system is idle!
#!/bin/bash
log()
{
echo "$(date): $@" >> /tmp/autosuspend.log
}
load_threshold()
{
DOTCONFIG="$HOME/.autosuspend"
if [ ! -e "$DOTCONFIG" ] ; then
echo "auto-creating $DOTCONFIG"
echo 5 > "$DOTCONFIG"
fi
IDLE_THRESHOLD_MIN=`cat $DOTCONFIG`
THRESHOLD_MS=$((IDLE_THRESHOLD_MIN * 60 * 1000))
log "suspend idle time threshold:\
$THRESHOLD_MS ms ($IDLE_THRESHOLD_MIN min)"
}
init()
{
IDLE_THRESHOLD_MIN=$1
INTERVAL_SEC=$2
[ -n "$INTERVAL_SEC" ] || INTERVAL_SEC=10
load_threshold
}
suspend_when_idle()
{
MAX_DIM_TIME_MS=$((1000 * 30))
while true ; do
sleep $INTERVAL_SEC
IDLE_TIME_MS=$((`xprintidle`+ 80))
if ((IDLE_TIME_MS > THRESHOLD_MS)) ; then
log "idle time: $IDLE_TIME_MS, suspending"
gnome-screensaver-command --lock
sudo pm-suspend
fi
done
}
monitor_threashold_change()
{
while true ; do
inotifywait $DOTCONFIG
load_threshold
done
}
init
monitor_threashold_change &
suspend_when_idle
@igal1c0de4n
Copy link
Author

Notes:

  1. Works on gnome-shell (only)
  2. Install dependencies (ubuntu-gnome 14.04):
    apt-get install xprintidle pm-utils
  3. sudo pm-suspend needs to run posswordless. One way is to create /etc/sudoers.d/power as below and add your user to the powerdev group

$ sudo cat sudoers.d/power
%powerdev ALL=NOPASSWD: /usr/sbin/pm-suspend,
/usr/sbin/pm-suspend-hybrid,
/usr/sbin/pm-hibernate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment