Skip to content

Instantly share code, notes, and snippets.

@RomkeVdMeulen
Last active August 29, 2015 14:14
Show Gist options
  • Save RomkeVdMeulen/534851ddd3553d2d9eeb to your computer and use it in GitHub Desktop.
Save RomkeVdMeulen/534851ddd3553d2d9eeb to your computer and use it in GitHub Desktop.
#!/bin/bash
# Auto suspend and wake-up script
#
# Puts the computer to sleep and automatically wakes it up at specified time
#
# Written by Romke van der Meulen <romke.vd.meulen@gmail.com>
#
# Takes a 24-hour time HH:MM as its argument
# Example:
# suspend_until 9:30
# suspend_until 18:45
# ------------------------------------------------------
# Argument check
if [ $# -lt 1 ]; then
echo "Usage: suspend_until HH:MM"
fi
# Check whether specified time today or tomorrow
DESIRED=$((`date +%s -d "$1"`+7200))
NOW=$((`date +%s`))
if [ $DESIRED -lt $NOW ]; then
DESIRED=$((DESIRED + 24*60*60))
fi
# Kill rtcwake if already running
sudo killall rtcwake
# Set RTC wakeup time
sudo rtcwake -t $DESIRED -m on &
# feedback
echo "Suspending..."
# give some time to cancel if needed
sleep 2
# then suspend
sudo pm-suspend
# Wake up with monitor off
xset dpms force off
# Kill rtcwake if still running
sudo killall rtcwake
# Any commands you want to launch after wakeup can be placed here
# Remember: sudo may have expired by now
clear
echo "Good morning!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment