#!/bin/bash #This is a script that acts a a sleep timer or basic alarm. #It is written for crunchbang linux installs--but should move #easily to most any linux distributions. #For most users you'll want to edit the series of commands #below to use whatever programs/utilities you favor ######DEFAULT COMMANDS##### #The path to the alarm noise (e.g. a beep, song, etc.) NOISE=~/music/WhatIGot.m4a #Program for playing the alarm ALARM="vlc --repeat $NOISE" #Screen locking command SCREENLOCK="xscreensaver-command -lock" #The suspending command SUSPEND="pm-suspend" #The hibernation command HIBERNATE="pm-hibernate" #Command or series of commands for shutting down SHUTDOWN="gdm-control --shutdown && openbox --exit" #Common use scripts (1st a backup script) You can #add more here and then duplicate lines 147-9, and edit accordingly. BACKUPSCRIPT="/home/jake/bin/remotebackup.sh" ########SCRIPT GENERAL INPUTS##### #Select action to take PS3="Choose:" echo "" echo "What action would you like to take at the end?" select act in "suspend" "hibernate" "lock screen" "shutdown" "play alarm" do break done ################################# #Translating the "acts" to proper commands ################################# if [ "$act" = "suspend" ]; then actcommand=$SUSPEND fi if [ "$act" = "hibernate" ]; then actcommand=$HIBERNATE fi if [ "$act" = "lock screen" ]; then actcommand=$SCREENLOCK fi if [ "$act" = "shutdown" ]; then actcommand=$SHUTDOWN fi if [ "$act" = "play alarm" ]; then actcommand=$ALARM fi ##Asking for passwords for those commands which need root privileges if [ "$actcommand" = "$SUSPEND" ] || [ "$actcommand" = "$HIBERNATE" ]; then echo "Selected action requires root privileges." stty_orig=`stty -g` stty -echo read -p "Enter sudo password:" pass stty $stty_orig fi #Select when you would like to do it PS3="Choose: " echo "" echo "When would you like to do it?" select when in "after time" "after task" "whichever is later" do break done echo "" #Show the current alarm path if [ "$act" = "play alarm" ]; then echo "Current path of alarm is set to $NOISE" echo "" fi #For 'whens' that require a time input if [ "$when" = "after time" ] || [ "$when" = "whichever is later" ]; then read -p "How long of a delay (in minutes): " minutes echo "" sectime=`date +%s` secoff=$(($sectime+($minutes*60))) dateoff=`date --date=@$secoff` fi ######################## #####The After Time Routine ######################## if [ "$when" = "after time" ]; then #The Main Timer elapsedtime=1 echo "" echo "" echo "Computer will $act at $dateoff." echo -n "Minutes left..." echo -n "$minutes..." while [ $elapsedtime -lt $minutes ] do sleep 1m echo -n "$(($minutes-$elapsedtime)).." elapsedtime=$(($elapsedtime+1)) done #The last 60 seconds.... echo "" echo "" if [ $minutes -gt 0 ]; then echo "Will $act in (seconds)..." lastsixty=1 while [ $lastsixty -le 60 ] do sleep 1s echo -n "$((60-$lastsixty))..." lastsixty=$(($lastsixty+1)) done fi #The Action if [ "$actcommand" = "$SUSPEND" ] || [ "$actcommand" = "$HIBERNATE" ]; then echo "$pass" | sudo -S $actcommand exit 0 else $actcommand exit 0 fi fi ##################### #Inputing the task to follow ##################### if [ "$when" = "after task" ] || [ "$when" = "whichever is later" ]; then PS3="Choose: " echo "" echo "Which task do you want to follow?" select task in "Already running task (pid)" "A new task" "Backup script" do break done if [ "$task" = "Already running task (pid)" ]; then read -p "Enter pid of task" taskpid fi if [ "$task" = "A new task" ]; then taskpid=0 read -p "Enter (terminal) command to run: " taskcommand fi if [ "$task" = "Backup script" ]; then taskpid=0 taskcommand=$BACKUPSCRIPT fi fi ######################## #####The After Task Routine ######################## if [ "$when" = "after task" ]; then if [ "$taskpid" = "0" ]; then terminator --command="$taskcommand" & clear taskpid=$! fi RUNNING=1 while [ $RUNNING -ge 1 ] do echo "Task still running" sleep 5s RUNNING=`ps $taskpid | grep -c $taskpid` done echo "Task complete...will now $act ." if [ "$actcommand" = "$SUSPEND" ] || [ "$actcommand" = "$HIBERNATE" ]; then echo "$pass" | sudo -S $actcommand exit 0 else $actcommand exit 0 fi fi ############################### #####The Whichever Is Later Routine ############################### if [ "$when" = "whichever is later" ]; then if [ "$taskpid" = "0" ]; then terminator --command="$taskcommand" & taskpid=$! fi RUNNING=1 while [ $RUNNING -ge 1 ] do echo "Task still running..." sleep 5s RUNNING=`ps $taskpid | grep -c $taskpid` clear done echo "" echo "Task complete...comparing with set $dateoff ." postbackuptime=`date +%s` if [ $secoff -gt $postbackuptime ]; then timedifference=$(($secoff-($postbackuptime))) echo "" echo "Sleeping for $timedifference seconds" sleep $timedifference fi if [ "$actcommand" = "$SUSPEND" ] || [ "$actcommand" = "$HIBERNATE" ]; then echo "$pass" | sudo -S $actcommand exit 0 else $actcommand exit 0 fi fi exit #end