Update (as of 27.03.12)
Another usage option added, as described in the script. Improved argument check.
Added the ability to postpone alert.
Note you have to edit optional pre-suspend and post-suspend commands; I left mine for example purposes.
Code:
#!/bin/bash
# Auto suspend and wake-up script. It's intended to act as an alarm-clock.
#
# Puts the computer on standby (after pre-defined time) and automatically wakes it up and executes commands at specified time or
# executes commands after given sleep period.
#
# Written by Romke van der Meulen <redge.online at gmail>
# Edited by Laur Aliste <laur.aliste at gmail>
#
# -----------------------------------------------------
# If you want the script to work without typing any passwords, you have to edit sudoers file:
# sudo visudo
# go to the last line and enter this line (without the '# ' at the beginning!) and save:
# username ALL=NOPASSWD: /usr/sbin/rtcwake
#
# !!! This step is only needed if you're going to use (long) pre-suspend sleep periods; if so, sudo might
# expire before 'sudo rtcwake' command is executed and system won't suspend !!!
# -----------------------------------------------------
##### USAGE #####
#
# 3 usage methods:
# 1. Suspending to RAM:
# Example: suspend_script 930 15 - suspends system after 15 min and wakes up at 9:30
# suspend_script 1845 10 - suspends system after 10 min and wakes up at 18:45
# suspend_script 0325 2 - suspends system after 2 min and wakes up at 03:25
# suspend_script 515 - If the second argument (pre-suspend sleep time) is missing as it is in this example,
# the default value is 10 sec, so the system suspends after 10 sec
# and wakes up at 05:15
# 2. Sleeping until predefined time:
# Example: suspend_script 930 sleep - executes pre-defined commands at 9:30
#
# 3. Sleeping for given time:
# Example: suspend_script sleep 31 - executes pre-defined commands after sleeping for 31 minutes
# suspend_script sleep - If the second argument (sleep time) is missing as it is in this example,
# the default value is 10 min, so the system executes pre-defined commands
# after sleeping for 10 minutes
#
#
# - You can place this file (suspend_script) into /usr/local/bin. When you want to use the script, open terminal
# and type "suspend_script TIMEVALUE SLEEPVALUE" or
# - simply drag the scriptfile (located anywhere) into terminal window and add those two values to the end of command line.
#
# Don't forget to make this file executable!
# ------------------------------------------------------
# Default values; do not change:
standalone_sleep=0
suspend=1
# Argument check:
if [[ $# > 2 ]]; then
echo "==============================================================================="
echo " Error: too many arguments;"
echo " only wake-up time and/or sleep arguments are accepted"
echo "==============================================================================="; exit 1
fi
if [[ $# < 1 ]]; then
echo "====================================================="
echo " Wake-up time (HHMM) or \"sleep\" needed"
echo "====================================================="; exit 1
fi
if [[ "$1" == "sleep" ]] ; then
standalone_sleep=1
suspend=0
if ! [[ -z $2 ]]; then
if ! [[ "$2" =~ ^[0-9]+$ ]] ; then
echo "========================================================================="
echo " Error: sleep value (MM) has to be numerical."
echo " eg. 10; 200; 5"
echo "========================================================================="; exit 1
fi
fi
elif ! [[ "$1" =~ ^[0-9]+$ ]]; then
echo "=============================================================="
echo " Error: first argument has to be either numerical (HHMM)"
echo " eg. 1000; 900; 1830; or \"sleep\""
echo "=============================================================="; exit 1
elif ! [[ -z $2 ]]; then
if [[ "$2" == "sleep" ]] ; then
suspend=0
elif ! [[ "$2" =~ ^[0-9]+$ ]] ; then
echo "========================================================================="
echo " Error: second argument has to be either numerical (MM)"
echo " eg. 10; 200; 5; or \"sleep\""
echo "========================================================================="; exit 1
fi
fi
# Check whether specified time is today or tomorrow and define suspend period:
if [[ $standalone_sleep == 0 ]]; then
TARGET1=$((`date +%s -d "$1"`))
NOW=$((`date +%s`))
if [ $TARGET1 -lt $NOW ]; then
TARGET1=$((`date +%s -d "$1"` + 24*60*60))
fi
TARGET=$(($TARGET1-$NOW))
fi
# Define hours/mins/sec until wake-up:
hours=$(($TARGET/3600))
minutes=$(( ($TARGET-($hours*3600))/60 ))
seconds=$(( $TARGET-(($hours*3600)+($minutes*60)) ))
# Check whether entered wake-up time makes any sense:
if [[ "$1" =~ ^[0-9]+$ ]] ; then
if (( $hours < 0 )); then
echo "======================================================"
echo " Error: please re-check the wake-up time"
echo "======================================================"; exit 1
fi
fi
# Decide whether to sleep or suspend; define pre-suspend sleep durance if necessary:
if [[ $suspend == 1 ]]; then
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#-------------------------------#-# SUSPEND SCRIPT #-#--------------------------------#
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
killall rtcwake > /dev/null 2>&1
if [[ $2 > 0 ]];then
sleep_value=$(( $2*60 ))
else
sleep_value=10
fi
# Print countdown feedback:
while [ $sleep_value -gt 0 ]; do
clear
# Let user know what we're going to do
echo "Going to suspend for $hours h $minutes min"
let sleep_value=$sleep_value-1
hours2=$(( $sleep_value/3600 ))
minutes2=$(( ($sleep_value-$hours2*3600)/60 ))
seconds2=$(( $sleep_value-($hours2*3600+$minutes2*60) ))
echo "----------------------------------------------------------"
if [[ $hours2 > 0 ]] ;then
echo "To cancel, press Ctrl+c within the next $hours2 h $minutes2 min $seconds2 sec"
elif [[ $minutes2 > 0 ]] ;then
echo "To cancel, press Ctrl+c within the next $minutes2 min $seconds2 sec"
else
echo "To cancel, press Ctrl+c within the next $seconds2 sec"
fi
sleep 1
done
#################################################################################################
#################################################################################################
# Between these double octothorpe (#) lines go optional commands
# which are to be executed BEFORE suspension:
#------------------------------------------------------------------------------------------------
# Kill conky:
#pkill -9 -f conky > /dev/null 2>&1
#################################################################################################
#################################################################################################
# Feedback again:
echo "Suspending..."
sleep 1
# Set RTC wakeup time and suspend to RAM:
sudo rtcwake -m mem -s $TARGET
# Buffer time...
sleep 2
#################################################################################################
#################################################################################################
# Between these double octothorpe (#) lines go optional commands
# which are to be executed AFTER suspension:
#------------------------------------------------------------------------------------------------
# Wake up with monitor disabled:
# xset dpms force off
# Clear the terminal window:
clear
# Set volume level and (just in case) unmute system:
amixer -c 0 set Master 80% unmute > /dev/null 2>&1
amixer -c 0 set PCM 80% unmute > /dev/null 2>&1
# Start conky:
#conky -c /home/laur/.conkyrc > /dev/null 2>&1
#conky -c /home/laur/.conkyrc_banshee > /dev/null 2>&1
echo "
Good morning
"
# Start ncmpcpp:
ncmpcpp play > /dev/null 2>&1
# Postpone? :
echo -n "Want to sleep some more? (y/n) "
read postpone
if [[ "$postpone" == "y" ]]; then
while [[ "$postpone" == "y" ]]; do
ncmpcpp pause > /dev/null 2>&1
echo -n "Enter time in minutes (leave blank for 10min): "
read sleep_value
if ! [[ -z $sleep_value ]]; then
if ! [[ "$sleep_value" =~ ^[0-9]+$ ]] ; then
echo "========================================================================="
echo " Error: sleep time (MM) has to be numerical."
echo " eg. 10; 20; 5"
echo "========================================================================="; exit 1
fi
fi
if [[ $sleep_value > 0 ]];then
sleep_value=$(( $sleep_value*60 ))
else
sleep_value=600
fi
# Define hours/mins/sec until wake-up:
hours=$(( $sleep_value/3600 ))
minutes=$(( ($sleep_value-$hours*3600)/60 ))
seconds=$(( $sleep_value-($hours*3600+$minutes*60) ))
while [ $sleep_value -gt 0 ]; do
clear
# Let user know what we're going to do
if [[ $hours > 0 ]] ;then
echo "Going to sleep for $hours h $minutes min"
else
echo "Going to sleep for $minutes min"
fi
let sleep_value=$sleep_value-1
hours2=$(( $sleep_value/3600 ))
minutes2=$(( ($sleep_value-$hours2*3600)/60 ))
seconds2=$(( $sleep_value-($hours2*3600+$minutes2*60) ))
echo "----------------------------------------------------------"
if [[ $hours2 > 0 ]] ;then
echo "Waking up in... $hours2 h $minutes2 min $seconds2 sec"
elif [[ $minutes2 > 0 ]] ;then
echo "Waking up in... $minutes2 min $seconds2 sec"
else
echo "Waking up in... $seconds2 sec"
fi
sleep 1
done
amixer -c 0 set Master 80% unmute > /dev/null 2>&1
amixer -c 0 set PCM 80% unmute > /dev/null 2>&1
ncmpcpp play > /dev/null 2>&1
echo -n "Want to sleep some more? (y/n) "
read postpone
done
fi
#################################################################################################
#################################################################################################
# Unload rtcwake if it's running, otherwise next time computer won't wake up from sleep:
killall rtcwake > /dev/null 2>&1
exit 1
fi
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#-------------------------------#-# SLEEP SCRIPT #-#--------------------------------#
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#################################################################################################
#################################################################################################
# Between these double octothorpe (#) lines go optional commands
# which are to be executed BEFORE system sleep:
#------------------------------------------------------------------------------------------------
#################################################################################################
#################################################################################################
if [[ "$standalone_sleep" == "1" ]]; then
if [[ $2 > 0 ]];then
sleep_value=$(( $2*60 ))
else
sleep_value=600
fi
# Define hours/mins/sec until wake-up:
hours=$(( $sleep_value/3600 ))
minutes=$(( ($sleep_value-$hours*3600)/60 ))
seconds=$(( $sleep_value-($hours*3600+$minutes*60) ))
while [ $sleep_value -gt 0 ]; do
clear
# Let user know what we're going to do
if [[ $hours > 0 ]] ;then
echo "Going to sleep for $hours h $minutes min"
else
echo "Going to sleep for $minutes min"
fi
let sleep_value=$sleep_value-1
hours2=$(( $sleep_value/3600 ))
minutes2=$(( ($sleep_value-$hours2*3600)/60 ))
seconds2=$(( $sleep_value-($hours2*3600+$minutes2*60) ))
echo "----------------------------------------------------------"
if [[ $hours2 > 0 ]] ;then
echo "Waking up in... $hours2 h $minutes2 min $seconds2 sec"
elif [[ $minutes2 > 0 ]] ;then
echo "Waking up in... $minutes2 min $seconds2 sec"
else
echo "Waking up in... $seconds2 sec"
fi
sleep 1
done
else
sleep_value=$TARGET
while [ $sleep_value -gt 0 ]; do
clear
# Let user know what we're going to do
if [[ $hours > 0 ]] ;then
echo "Going to sleep for $hours h $minutes min $seconds s"
elif [[ $minutes > 0 ]] ;then
echo "Going to sleep for $minutes min $seconds s"
else
echo "Going to sleep for $seconds s"
fi
let sleep_value=$sleep_value-1
hours2=$(( $sleep_value/3600 ))
minutes2=$(( ($sleep_value-$hours2*3600)/60 ))
seconds2=$(( $sleep_value-($hours2*3600+$minutes2*60) ))
echo "----------------------------------------------------------"
if [[ $hours2 > 0 ]] ;then
echo "Waking up in... $hours2 h $minutes2 min $seconds2 sec"
elif [[ $minutes2 > 0 ]] ;then
echo "Waking up in... $minutes2 min $seconds2 sec"
else
echo "Waking up in... $seconds2 sec"
fi
sleep 1
done
fi
#################################################################################################
#################################################################################################
# Between these double octothorpe (#) lines go optional commands
# which are to be executed AFTER system sleep:
#------------------------------------------------------------------------------------------------
clear
# Set volume level and (just in case) unmute system:
amixer -c 0 set Master 80% unmute > /dev/null 2>&1
amixer -c 0 set PCM 80% unmute > /dev/null 2>&1
# Start conky:
#conky -c /home/laur/.conkyrc > /dev/null 2>&1
#conky -c /home/laur/.conkyrc_banshee > /dev/null 2>&1
echo "
Wakey-wakey
"
#Start ncmpcpp:
ncmpcpp play > /dev/null 2>&1
# Postpone? :
echo -n "Want to sleep some more? (y/n) "
read postpone
if [[ "$postpone" == "y" ]]; then
while [[ "$postpone" == "y" ]]; do
ncmpcpp pause > /dev/null 2>&1
echo -n "Enter time in minutes (leave blank for 10min): "
read sleep_value
if ! [[ -z $sleep_value ]]; then
if ! [[ "$sleep_value" =~ ^[0-9]+$ ]] ; then
echo "========================================================================="
echo " Error: sleep time (MM) has to be numerical."
echo " eg. 10; 20; 5"
echo "========================================================================="; exit 1
fi
fi
if [[ $sleep_value > 0 ]];then
sleep_value=$(( $sleep_value*60 ))
else
sleep_value=600
fi
# Define hours/mins/sec until wake-up:
hours=$(( $sleep_value/3600 ))
minutes=$(( ($sleep_value-$hours*3600)/60 ))
seconds=$(( $sleep_value-($hours*3600+$minutes*60) ))
while [ $sleep_value -gt 0 ]; do
clear
# Let user know what we're going to do
if [[ $hours > 0 ]] ;then
echo "Going to sleep for $hours h $minutes min"
else
echo "Going to sleep for $minutes min"
fi
let sleep_value=$sleep_value-1
hours2=$(( $sleep_value/3600 ))
minutes2=$(( ($sleep_value-$hours2*3600)/60 ))
seconds2=$(( $sleep_value-($hours2*3600+$minutes2*60) ))
echo "----------------------------------------------------------"
if [[ $hours2 > 0 ]] ;then
echo "Waking up in... $hours2 h $minutes2 min $seconds2 sec"
elif [[ $minutes2 > 0 ]] ;then
echo "Waking up in... $minutes2 min $seconds2 sec"
else
echo "Waking up in... $seconds2 sec"
fi
sleep 1
done
amixer -c 0 set Master 80% unmute > /dev/null 2>&1
amixer -c 0 set PCM 80% unmute > /dev/null 2>&1
ncmpcpp play > /dev/null 2>&1
echo -n "Want to sleep some more? (y/n) "
read postpone
done
fi
#################################################################################################
#################################################################################################
exit 1
Bookmarks