Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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 18:45 10 - suspends system after 10 min and wakes up at 18:45
- # suspend_script 03:25 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
- #
- # This scripts provides also default wake-up time, in case you forget to set the alarm.
- # To use this functionality, set cron to run this script at the time you would like to get the default alarm with the 'automated' flag, for instance:
- #
- # 30 07 * * * /usr/local/bin/suspend_script automated
- #
- # would wake you up at 07:30, in case you forgot to set the clock. (this assumes the computer is running and 'notify-send' and 'zenity' are installed)
- #
- #
- #
- # - 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!
- # ------------------------------------------------------
- command_play="ncmpcpp play" # Your command, that starts the (music) player. This example starts ncmpcpp player.
- command_pause="ncmpcpp pause" # Command, that pauses the player defined in the above command.
- volume="80" # Volume percentage on alarm start (without the '%' sign). Note it sets the sound card, not your program volume level.
- # ------------------------------------------------------
- export DISPLAY=:0.0
- # Default values; do not change:
- standalone_sleep=0
- suspend=1
- # A CONVENIENCE VARIABLE (same as running `basename $0`)
- zero=${0##*/}
- DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
- # Here we create and manage a file containing PIDs of all our running instances of the this script:
- global_pidlist_file=$HOME/.PIDs/$zero.pids
- if [ ! -d $HOME/.PIDs ]; then
- mkdir $HOME/.PIDs
- fi
- # PID collection:
- printf "$$\n" >>$global_pidlist_file
- trap "sed -i /$$/d $global_pidlist_file" EXIT
- #====================== ALARM ROUTINE ==========================
- function alert {
- # Wake up with monitor disabled:
- # xset dpms force off
- # Clear the terminal window:
- clear
- # Wait for internet connection:
- sleep 5
- # Display current WX (requires metar script to be installed):
- metar EETU
- # Set volume level and (just in case) unmute system:
- amixer -c 0 set Master playback $volume% unmute > /dev/null 2>&1
- #amixer -c 0 set PCM playback $volume% unmute > /dev/null 2>&1
- echo -e "$welcome_message"
- # Start ncmpcpp:
- $command_play > /dev/null 2>&1
- # Postpone? :
- echo -e "Want to sleep some more? (y/n)\n"
- read postpone
- while [[ "$postpone" == "y" ]]; do
- $command_pause > /dev/null 2>&1
- amixer -c 0 set Master playback 60% > /dev/null 2>&1
- #amixer -c 0 set PCM 60% > /dev/null 2>&1
- clear
- echo -e "Enter time in minutes (leave blank for 10min):\n"
- read sleep_value
- if ! [[ -z $sleep_value ]]; then
- if ! [[ "$sleep_value" =~ ^[0-9]+$ ]] ; then
- until [[ "$sleep_value" =~ ^[0-9]+$ ]] ; do
- clear
- echo "========================================================================="
- echo " Error: sleep time (MM) has to be numerical."
- echo " eg. 10; 20; 5. Try again."
- echo "========================================================================="
- echo -e "Enter time in minutes:\n"
- read sleep_value
- done
- 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 playback $volume% unmute > /dev/null 2>&1
- #amixer -c 0 set PCM $volume% unmute > /dev/null 2>&1
- $command_play > /dev/null 2>&1
- clear
- echo -e "Want to sleep some more? (y/n)\n"
- read postpone
- done
- # Unload rtcwake if it's running, otherwise next time computer won't wake up from sleep:
- killall rtcwake > /dev/null 2>&1
- exit
- }
- #==============================================================
- # Check whether the help output is requested:
- case $1 in
- "-h" | "--help" )
- clear
- printf "
- Auto suspend and wake-up script. It's intended to act as an alarm-clock.
- 3 usage methods:
- 1. Suspending to RAM:
- Example: '$zero 930 15' - suspends system after 15 min
- and wakes up at 9:30
- '$zero 18:45 10' - suspends system after 10 min
- and wakes up at 1845
- '$zero 0325 2' - suspends system after 2 min
- and wakes up at 03:25
- '$zero 5:15' - 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 (i.e. without suspending):
- Example: '$zero 930 sleep' - executes pre-defined commands at 9:30
- '$zero 10:20 sleep 60' - if there's third argument defined, it
- will describe volume percentage, so the
- alarm will set off at 10:20 with 60 perc. vol.
- 3. Sleeping for given time:
- Example: '$zero sleep 31' - executes pre-defined commands after
- sleeping for 31 minutes
- '$zero sleep 31 75' - same case as above - if there's third
- argument, it'll be the volume percentage.
- '$zero 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
- This scripts provides also default wake-up time, in case you forget to set the
- alarm. To use this functionality, set cron to run this script at the time you
- would like to get the default alarm with the 'automated' flag, for instance:
- 30 07 * * * /usr/local/bin/$zero automated
- would wake you up at 07:30, in case you forgot to set the clock. (this assumes
- the computer is running and 'notify-send' and 'zenity' are installed)
- - You can place this file ($zero) into /usr/local/bin.
- When you want to use the script, open terminal and type
- '$zero 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!
- -------------------------------------------------------------------
- "; exit 0
- ;;
- esac
- # Argument check:
- if [[ $# > 3 ]]; then
- clear
- echo "==============================================================================="
- echo " Error: too many arguments;"
- echo " only wake-up time, sleep or volume arguments are accepted"
- echo "==============================================================================="; exit 1
- elif [[ $# < 1 ]]; then
- clear
- echo "====================================================="
- echo " Wake-up time (HHMM) or \"sleep\" needed"
- echo "====================================================="; exit 1
- elif [[ "$1" == "automated" ]]; then
- pid_count="$(cat $global_pidlist_file | sed '/^\s*#/d;/^\s*$/d' | wc -l)"
- # Start the default wake-up-reel, if pid count = 1 (i.e. wake-up time was not previously defined, so other instances are not running):
- if [[ "$pid_count" == "1" ]]; then
- # Just in case display a message of oncoming alarm:
- auto_abort=$(zenity --entry --width=300 --height=400 --timeout=10 --text="Enter any text and press OK in 10sec to abort the automatic wake-up!")
- if ! [[ -z "$auto_abort" ]]; then
- notify-send -t 5000 -i gtk-dialog-info "Automatic wake-up alarm aborted."
- else
- # Set volume level and (just in case) unmute system:
- amixer -c 0 set Master playback $volume% unmute > /dev/null 2>&1
- #amixer -c 0 set PCM $volume% unmute > /dev/null 2>&1
- # Start ncmpcpp:
- $command_play > /dev/null 2>&1
- # Postpone? :
- postpone=$(zenity --entry --width=400 --height=400 --text="Want to sleep some more? (y/n)")
- while [[ "$postpone" == "y" ]]; do
- $command_pause > /dev/null 2>&1
- amixer -c 0 set Master playback 60% > /dev/null 2>&1
- #amixer -c 0 set PCM 60% > /dev/null 2>&1
- sleep_value=$(zenity --entry --width=400 --height=400 --text="Enter time in minutes (leave blank for 10min)")
- if ! [[ -z $sleep_value ]]; then
- until [[ "$sleep_value" =~ ^[0-9]+$ ]] ; do
- zenity --error --width=400 --height=400 --text "Error: sleep time (MM) has to be numerical, eg. 10; 20; 5"
- sleep_value=$(zenity --entry --width=400 --height=400 --text="Enter time in minutes:")
- done
- fi
- if [[ $sleep_value > 0 ]];then
- sleep_value=$(( $sleep_value*60 ))
- else
- sleep_value=600
- fi
- notify-send -t 5000 -i gtk-dialog-info "Ok, sleeping for another $(( $sleep_value / 60 )) minutes."
- sleep $sleep_value
- amixer -c 0 set Master playback $volume% unmute > /dev/null 2>&1
- #amixer -c 0 set PCM $volume% unmute > /dev/null 2>&1
- $command_play > /dev/null 2>&1
- postpone=$(zenity --entry --width=400 --height=400 --text="Want to sleep some more? (y/n)")
- done
- # Unload rtcwake if it's running, otherwise next time computer won't wake up from sleep:
- killall rtcwake > /dev/null 2>&1
- fi
- fi
- exit 0
- elif [[ "$1" == "sleep" ]] ; then
- standalone_sleep=1
- suspend=0
- if ! [[ -z $2 ]]; then
- if ! [[ "$2" =~ ^[0-9]+$ ]] ; then
- clear
- echo "========================================================================="
- echo " Error: sleep value (MM) has to be numerical."
- echo " eg. 10; 200; 5"
- echo "========================================================================="; exit 1
- fi
- fi
- if ! [[ -z $3 ]]; then
- if ! [[ "$3" =~ ^[0-9]+$ ]] ; then
- clear
- echo "========================================================================="
- echo " Error: volume percentage has to be numerical."
- echo " eg. 50; 60; 75"
- echo "========================================================================="; exit 1
- else
- volume="$3"
- fi
- fi
- elif ! [[ "$1" =~ ^[0-9:]+$ ]]; then
- clear
- echo "=============================================================="
- echo " Error: first argument has to be either numerical (HHMM)"
- echo " eg. 1000; 900; 18:30; or \"sleep\""
- echo "=============================================================="; exit 1
- elif ! [[ -z $2 ]]; then
- if [[ "$2" == "sleep" ]] ; then
- suspend=0
- if ! [[ -z $3 ]]; then
- if ! [[ "$3" =~ ^[0-9]+$ ]] ; then
- clear
- echo "========================================================================="
- echo " Error: volume percentage has to be numerical."
- echo " eg. 50; 60; 75"
- echo "========================================================================="; exit 1
- else
- volume="$3"
- fi
- fi
- elif ! [[ "$2" =~ ^[0-9]+$ ]] ; then
- clear
- 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
- TARGET2=$(($TARGET1-$NOW))
- fi
- # Check whether the entered wake-up time makes any sense (this protects against ill-logical time values, as 1971, 2600 etc):
- if [[ "$1" =~ ^[0-9:]+$ ]] ; then
- # Define hours/mins/sec until wake-up:
- hours=$(($TARGET2/3600))
- minutes=$(( ($TARGET2-($hours*3600))/60 ))
- seconds=$(( $TARGET2-(($hours*3600)+($minutes*60)) ))
- if (( $hours < 0 )); then
- clear
- echo "======================================================"
- echo " Error: please re-check the wake-up time"
- echo "======================================================"; exit 1
- fi
- mins_=$(date +%M -d "$1")
- hours_=$(date +%H -d "$1")
- 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
- # Subtract 'sleep_value' from 'TARGET', otherwise the computer will wake up at desired time + sleep_value:
- TARGET=$(( $TARGET2 - $sleep_value ))
- # 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 (waking up at $hours_:$mins_)"
- 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:
- #------------------------------------------------------------------------------------------------
- # Start conky:
- #conky -c $HOME/.conkyrc > /dev/null 2>&1
- conky -d -c $HOME/.conky/.conkyrc_error_reporting > /dev/null 2>&1
- welcome_message="\n Good morning\n\n\n"
- alert # This is the default alarm routine defined above. Deleting not recommended (provides the wake-up routine).
- #################################################################################################
- #################################################################################################
- elif [[ "$standalone_sleep" == "1" ]]; then
- #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
- #-------------------------------#-# SLEEP SCRIPT #-#--------------------------------#
- #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
- #################################################################################################
- #################################################################################################
- # Between these double octothorpe (#) lines go optional commands
- # which are to be executed BEFORE system sleep (standalone sleep):
- #------------------------------------------------------------------------------------------------
- #################################################################################################
- #################################################################################################
- 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
- #################################################################################################
- #################################################################################################
- # Between these double octothorpe (#) lines go optional commands
- # which are to be executed BEFORE system sleep (non-standalone sleep):
- #------------------------------------------------------------------------------------------------
- #################################################################################################
- #################################################################################################
- sleep_value=$TARGET2
- 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 (non-standalone and standalone sleep):
- #------------------------------------------------------------------------------------------------
- welcome_message="\n Wakey-wakey\n\n\n"
- alert # This is the default alarm routine defined above. Deleting not recommended (provides the wake-up routine).
- #################################################################################################
- #################################################################################################
- exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement