Posted by jmbarnes on Thu 27th Jan 21:02 (modification of post by view diff)
download | new post
- #!/bin/bash
- #Bash sleep timer
- #
- #Written by J. Barney.
- #
- #This script is originally made to be run
- #on a default installation of Crunchbang Linux (statler)
- #Depends upon: pm-utils, cvlc, xscreensaver, and bc
- #
- #Things you may need / want to edit:
- # *the functions for actions to take after the timer (suspend etc)
- # *command line flags in getopts section
- ########
- ########
- #Function for after-timer actions
- ########
- suspend ()
- {
- echo "$pass" | sudo -S pm-suspend
- #dbus-send --system --print-reply --dest=org.freedesktop.Hal \
- #/org/freedesktop/Hal/devices/computer \
- #org.freedesktop.Hal.Device.SystemPowerManagement.Suspend \
- #int32:0
- }
- hibernate ()
- {
- echo "$pass" | sudo -S pm-hibernate
- #Password-les dbus command
- #dbus-send --system --print-reply --dest=org.freedesktop.Hal \
- #/org/freedesktop/Hal/devices/computer \
- #org.freedesktop.Hal.Device.SystemPowerManagement.Hibernate \
- #int32:0
- }
- alarm ()
- {
- cvlc --repeat MUSICPATH
- vlicpid=$!
- sleep 2s
- clear
- read -p "Press enter to kill the alarm" dismiss
- kill $vlcpid
- }
- lock ()
- {
- xscreensaver-command -lock
- }
- shutdown ()
- {
- echo "$pass" | sudo -S poweroff
- }
- ########
- #Command line flag reading
- ########
- while getopts m:w:shatlp OPTION ; do
- case $OPTION in
- #Can be made more robust.
- m) MINUTES=$OPTARG ;;
- t) ROUTINE="timer" ;;
- w) ROUTINE="wait"
- PIDSCRIPT=$OPTARG ;;
- s) ACTION="suspend" ;;
- h) ACTION="hibernate" ;;
- a) ACTION="alarm" ;;
- l) ACTION="lock" ;;
- p) ACTION="shutdown" ;;
- \?) echo "Usage: timer.sh minutes -w wait -shalp"
- echo "Options: -shalp are exclusive. Specify one"
- echo "-s (suspend) -h (hibernate) -a (alarm) -l (lock)"
- echo "-p (poweroff)."
- echo "-m: requires number of minutes to wait (e.g. 50)"
- echo "-w: requires PID to watch or script to run"
- echo ""
- echo "Example: timer.sh -m 40 -w ~/backup.sh -s"
- echo "Will suspend after the backup or 40 minutes,"
- echo "whichever is later."
- exit 0
- ;;
- esac
- done
- ########
- #Assume default routine of timer unless specified otherwise)
- ########
- if [ "$ROUTINE" = "" ]; then
- ROUTINE="timer"
- fi
- ########
- #Getting neccesary unspecified variables
- ########
- if [ "$MINUTES" = "" ]; then
- read -p "How many minutes until action? " MINUTES
- echo ""
- fi
- if [ "$ACTION" = "" ]; then
- echo "No post-timer action specified."
- PS3="Choose:"
- echo "Select action from the following:"
- select ACTION in "suspend" "hibernate" "lock" "shutdown" "alarm"
- do
- break
- done
- echo ""
- fi
- ########
- #Getting root privleges if necessary
- ########
- #Code below prompts for password while hidding entry.
- #Password unnecessary with direct accessing of Hal/Dbus instead of
- #using the pm-suspend from command line.
- case $ACTION in
- shutdown | suspend | hibernate )
- echo ""
- echo "This action requires root privileges."
- stty_orig=`stty -g`
- stty -echo
- read -p "Enter sudo password:" pass
- stty $stty_orig
- echo ""
- ;;
- esac
- ########
- #A few handy calculations
- ########
- SECTIME=`date +%s`
- SECOFF=$(($SECTIME+($MINUTES*60)))
- DATEOFF=`date --date=@$SECOFF`
- echo "Will $ACTION after $MINUTES minutes ($DATEOFF)."
- ########
- #Running the routines
- ########
- case $ROUTINE in
- #The 'timer' routine
- timer )
- elapsedtime=0
- echo ""
- echo -n "Minutes left...$MINUTES..."
- while [ $elapsedtime -lt $MINUTES ]
- do
- sleep 1m
- echo -n "$(($MINUTES-$elapsedtime)).."
- elapsedtime=$(($elapsedtime+1))
- done
- ;;
- #The 'wait' routine
- wait )
- if [ "$PIDSCRIPT" -eq "$PIDSCRIPT" ]; then
- echo "You passed a PID ($PIDSCRIPT)"
- else
- echo "Running the passed script ($PIDSCRIPT) in a new terminal..."
- sleep 2s
- x-terminal-emulator --command="$PIDSCRIPT" &
- PIDSCRIPT=$!
- sleep 5s
- clear
- fi
- RUNNING=1
- echo ""
- echo -n "Task still running..."
- while [ $RUNNING -ge 1 ]
- do
- echo -n "..."
- sleep 5s
- RUNNING=`ps $PIDSCRIPT | grep -c $PIDSCRIPT`
- done
- echo "Task complete...comparing with set time $DATEOFF ."
- postbackuptime=`date +%s`
- if [ $SECOFF -gt $postbackuptime ]; then
- timedifference=$(($SECOFF-($postbackuptime)))
- minleft=$( echo "$timedifference/60" | bc )
- echo ""
- echo -n "Sleeping for $minleft minutes..."
- timeleft=1
- while [ "$timeleft" -le "$minleft" ]
- do
- sleep 1m
- echo -n "$(($minleft-$timeleft))..."
- timeleft=$(($timeleft+1))
- done
- fi
- ;;
- esac
- ########
- #A friendly warning before action
- ########
- echo ""
- echo -n "Will $ACTION in 10 seconds..."
- lastten=10
- while [ $lastten -gt 0 ]
- do
- sleep 1s
- lastten=$(($lastten-1))
- echo -n "$lastten..."
- done
- echo ""
- ########
- #Running the post-routine action (based on functions at top)
- ########
- $ACTION
- exit 0
- #end
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.