Skip to content

Instantly share code, notes, and snippets.

@werkshy
Created August 25, 2012 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save werkshy/3459241 to your computer and use it in GitHub Desktop.
Save werkshy/3459241 to your computer and use it in GitHub Desktop.
Sleeper: a tool to suspend your PC under lightweight window managers
#!/bin/bash
# Sleeper: a tool to suspend your PC under lightweight window managers
# By Andy O'Neill <andy@potatoriot.com> 2012
# Released to the public domain
# Depends on xprintidle & console-kit (apt-get install xprintidle console-kit)
# Usage: just launch it from your .xinitrc or whatever you use to launch your WM
# e.g. sleeper &
# Configuration:
# Set IDLE_SUSPEND_TIME in ms
# e.g. IDLE_SUSPEND_TIME=1800000 sleeper &
# Time in between idle checks in seconds
IDLE_CHECK_INTERVAL=60
DONT_SUSPEND_FILE=/tmp/DONT_SUSPEND_$DISPLAY
LABEL="Sleeper on $DISPLAY -"
if [ -z $IDLE_SUSPEND_TIME ]; then
echo "Choosing default idle suspend time: 1 hour"
IDLE_SUSPEND_TIME=3600000
fi
while true; do
# Get the active sessions
active_x_display=$(ck-list-sessions | grep -A 1 "active = TRUE" | tail -1 \
| awk -F "'" '{print $2}')
idle=$(xprintidle)
echo "$LABEL Active X display: $active_x_display Idle time: $idle ms"
if [ "$active_x_display" != "$DISPLAY" ]; then
echo "$LABEL Current display is not active, would not suspend"
elif [ -f $DONT_SUSPEND_FILE ]; then
echo "Sleeper $DISPLAY - idle time is $idle ms but \
$DONT_SUSPEND_FILE exists"
elif [ $idle -gt $IDLE_SUSPEND_TIME ];then
echo "$LABEL idle time is $idle ms, trying to suspend now"
sudo pm-suspend
fi
sleep $IDLE_CHECK_INTERVAL
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment