| | | |
| 1 | 1 | | #!/bin/bash |
| 2 | 2 | | #This is a script that acts a a sleep timer or basic alarm. |
| 3 | 3 | | #It is written for crunchbang linux installs--but should move |
| 4 | 4 | | #easily to most any linux distributions. |
| 5 | 5 | | #For most users you'll want to edit the series of commands |
| 6 | 6 | | #below to use whatever programs/utilities you favor |
| 8 | 8 | | ######DEFAULT COMMANDS##### |
| 10 | 10 | | #The path to the alarm noise (e.g. a beep, song, etc.) |
| 11 | 11 | | NOISE=~/music/WhatIGot.m4a |
| 12 | 12 | | #Program for playing the alarm |
| 13 | | - | ALARM="cvlc --repeat $NOISE && echo "Press CTRL-C to exit"" |
| 13 | + | ALARM="vlc --repeat $NOISE" |
| 14 | 14 | | #Screen locking command |
| 15 | 15 | | SCREENLOCK="xscreensaver-command -lock" |
| 16 | 16 | | #The suspending command |
| 17 | 17 | | SUSPEND="pm-suspend" |
| 18 | 18 | | #The hibernation command |
| 19 | 19 | | HIBERNATE="pm-hibernate" |
| 20 | 20 | | #Command or series of commands for shutting down |
| 21 | 21 | | SHUTDOWN="gdm-control --shutdown && openbox --exit" |
| 22 | 22 | | #Common use scripts (1st a backup script) You can |
| 23 | 23 | | #add more here and then duplicate lines 147-9, and edit accordingly. |
| 24 | 24 | | BACKUPSCRIPT="/home/jake/bin/remotebackup.sh" |
| 27 | 27 | | ########SCRIPT GENERAL INPUTS##### |
| 28 | 28 | | #Select action to take |
| 29 | 29 | | PS3="Choose:" |
| 30 | 30 | | echo "" |
| 31 | 31 | | echo "What action would you like to take at the end?" |
| 32 | 32 | | select act in "suspend" "hibernate" "lock screen" "shutdown" "play alarm" |
| 33 | 33 | | do |
| 34 | 34 | | break |
| 35 | 35 | | done |
| 37 | 37 | | ################################# |
| 38 | 38 | | #Translating the "acts" to proper commands |
| 39 | 39 | | ################################# |
| 40 | 40 | | if [ "$act" = "suspend" ]; then |
| 41 | 41 | | actcommand=$SUSPEND |
| 42 | 42 | | fi |
| 43 | 43 | | if [ "$act" = "hibernate" ]; then |
| 44 | 44 | | actcommand=$HIBERNATE |
| 45 | 45 | | fi |
| 46 | 46 | | if [ "$act" = "lock screen" ]; then |
| 47 | 47 | | actcommand=$SCREENLOCK |
| 48 | 48 | | fi |
| 49 | 49 | | if [ "$act" = "shutdown" ]; then |
| 50 | 50 | | actcommand=$SHUTDOWN |
| 51 | 51 | | fi |
| 52 | 52 | | if [ "$act" = "play alarm" ]; then |
| 53 | 53 | | actcommand=$ALARM |
| 54 | 54 | | fi |
| 56 | 56 | | ##Asking for passwords for those commands which need root privileges |
| 57 | 57 | | if [ "$actcommand" = "$SUSPEND" ] || [ "$actcommand" = "$HIBERNATE" ]; then |
| 58 | 58 | | echo "Selected action requires root privileges." |
| 59 | 59 | | stty_orig=`stty -g` |
| 60 | 60 | | stty -echo |
| 61 | 61 | | read -p "Enter sudo password:" pass |
| 62 | 62 | | stty $stty_orig |
| 63 | 63 | | fi |
| 65 | 65 | | #Select when you would like to do it |
| 66 | 66 | | PS3="Choose: " |
| 67 | 67 | | echo "" |
| 68 | 68 | | echo "When would you like to do it?" |
| 69 | 69 | | select when in "after time" "after task" "whichever is later" |
| 70 | 70 | | do |
| 71 | 71 | | break |
| 72 | 72 | | done |
| 73 | 73 | | echo "" |
| 75 | 75 | | #Show the current alarm path |
| 76 | 76 | | if [ "$act" = "play alarm" ]; then |
| 77 | 77 | | echo "Current path of alarm is set to $NOISE" |
| 78 | 78 | | echo "" |
| 79 | 79 | | fi |
| 81 | 81 | | #For 'whens' that require a time input |
| 82 | 82 | | if [ "$when" = "after time" ] || [ "$when" = "whichever is later" ]; then |
| 83 | 83 | | read -p "How long of a delay (in minutes): " minutes |
| 84 | 84 | | echo "" |
| 85 | 85 | | sectime=`date +%s` |
| 86 | 86 | | secoff=$(($sectime+($minutes*60))) |
| 87 | 87 | | dateoff=`date --date=@$secoff` |
| 88 | 88 | | fi |
| 90 | 90 | | ######################## |
| 91 | 91 | | #####The After Time Routine |
| 92 | 92 | | ######################## |
| 93 | 93 | | if [ "$when" = "after time" ]; then |
| 94 | 94 | | #The Main Timer |
| 95 | 95 | | elapsedtime=1 |
| 96 | 96 | | echo "" |
| 97 | 97 | | echo "" |
| 98 | 98 | | echo "Computer will $act at $dateoff." |
| 99 | 99 | | echo -n "Minutes left..." |
| 100 | 100 | | echo -n "$minutes..." |
| 101 | 101 | | while [ $elapsedtime -lt $minutes ] |
| 102 | 102 | | do |
| 103 | 103 | | sleep 1m |
| 104 | 104 | | echo -n "$(($minutes-$elapsedtime)).." |
| 105 | 105 | | elapsedtime=$(($elapsedtime+1)) |
| 106 | 106 | | done |
| 107 | 107 | | #The last 60 seconds.... |
| 108 | 108 | | echo "" |
| 109 | 109 | | echo "" |
| 110 | 110 | | if [ $minutes -gt 0 ]; then |
| 111 | 111 | | echo "Will $act in (seconds)..." |
| 112 | 112 | | lastsixty=1 |
| 113 | 113 | | while [ $lastsixty -le 60 ] |
| 114 | 114 | | do |
| 115 | 115 | | sleep 1s |
| 116 | 116 | | echo -n "$((60-$lastsixty))..." |
| 117 | 117 | | lastsixty=$(($lastsixty+1)) |
| 118 | 118 | | done |
| 119 | 119 | | fi |
| 120 | 120 | | #The Action |
| 121 | 121 | | if [ "$actcommand" = "$SUSPEND" ] || [ "$actcommand" = "$HIBERNATE" ]; then |
| 122 | 122 | | echo "$pass" | sudo -S $actcommand |
| 123 | 123 | | exit 0 |
| 124 | 124 | | else |
| 125 | 125 | | $actcommand |
| 126 | 126 | | exit 0 |
| 127 | 127 | | fi |
| 128 | 128 | | fi |
| 130 | 130 | | ##################### |
| 131 | 131 | | #Inputing the task to follow |
| 132 | 132 | | ##################### |
| 133 | 133 | | if [ "$when" = "after task" ] || [ "$when" = "whichever is later" ]; then |
| 134 | 134 | | PS3="Choose: " |
| 135 | 135 | | echo "" |
| 136 | 136 | | echo "Which task do you want to follow?" |
| 137 | 137 | | select task in "Already running task (pid)" "A new task" "Backup script" |
| 138 | 138 | | do |
| 139 | 139 | | break |
| 140 | 140 | | done |
| 141 | 141 | | if [ "$task" = "Already running task (pid)" ]; then |
| 142 | 142 | | read -p "Enter pid of task" taskpid |
| 143 | 143 | | fi |
| 144 | 144 | | if [ "$task" = "A new task" ]; then |
| 145 | 145 | | taskpid=0 |
| 146 | 146 | | read -p "Enter (terminal) command to run: " taskcommand |
| 147 | 147 | | fi |
| 148 | 148 | | if [ "$task" = "Backup script" ]; then |
| 149 | 149 | | taskpid=0 |
| 150 | 150 | | taskcommand=$BACKUPSCRIPT |
| 151 | 151 | | fi |
| 152 | 152 | | fi |
| 154 | 154 | | ######################## |
| 155 | 155 | | #####The After Task Routine |
| 156 | 156 | | ######################## |
| 157 | 157 | | if [ "$when" = "after task" ]; then |
| 158 | 158 | | if [ "$taskpid" = "0" ]; then |
| 159 | 159 | | terminator --command="$taskcommand" & |
| 160 | 160 | | clear |
| 161 | 161 | | taskpid=$! |
| 162 | 162 | | fi |
| 163 | 163 | | RUNNING=1 |
| 164 | 164 | | while [ $RUNNING -ge 1 ] |
| 165 | 165 | | do |
| 166 | 166 | | echo "Task still running" |
| 167 | 167 | | sleep 5s |
| 168 | 168 | | RUNNING=`ps $taskpid | grep -c $taskpid` |
| 169 | 169 | | done |
| 170 | 170 | | echo "Task complete...will now $act ." |
| 171 | 171 | | if [ "$actcommand" = "$SUSPEND" ] || [ "$actcommand" = "$HIBERNATE" ]; then |
| 172 | 172 | | echo "$pass" | sudo -S $actcommand |
| 173 | 173 | | exit 0 |
| 174 | 174 | | else |
| 175 | 175 | | $actcommand |
| 176 | 176 | | exit 0 |
| 177 | 177 | | fi |
| 178 | 178 | | fi |
| 180 | 180 | | ############################### |
| 181 | 181 | | #####The Whichever Is Later Routine |
| 182 | 182 | | ############################### |
| 183 | 183 | | if [ "$when" = "whichever is later" ]; then |
| 184 | 184 | | if [ "$taskpid" = "0" ]; then |
| 185 | 185 | | terminator --command="$taskcommand" & |
| 186 | 186 | | taskpid=$! |
| 187 | 187 | | fi |
| 188 | 188 | | RUNNING=1 |
| 189 | 189 | | while [ $RUNNING -ge 1 ] |
| 190 | 190 | | do |
| 191 | 191 | | echo "Task still running..." |
| 192 | 192 | | sleep 5s |
| 193 | 193 | | RUNNING=`ps $taskpid | grep -c $taskpid` |
| 194 | 194 | | clear |
| 195 | 195 | | done |
| 196 | 196 | | echo "" |
| 197 | 197 | | echo "Task complete...comparing with set $dateoff ." |
| 198 | 198 | | postbackuptime=`date +%s` |
| 199 | 199 | | if [ $secoff -gt $postbackuptime ]; then |
| 200 | 200 | | timedifference=$(($secoff-($postbackuptime))) |
| 201 | 201 | | echo "" |
| 202 | 202 | | echo "Sleeping for $timedifference seconds" |
| 203 | 203 | | sleep $timedifference |
| 204 | 204 | | fi |
| 205 | 205 | | if [ "$actcommand" = "$SUSPEND" ] || [ "$actcommand" = "$HIBERNATE" ]; then |
| 206 | 206 | | echo "$pass" | sudo -S $actcommand |
| 207 | 207 | | exit 0 |
| 208 | 208 | | else |
| 209 | 209 | | $actcommand |
| 210 | 210 | | exit 0 |
| 211 | 211 | | fi |
| 212 | 212 | | fi |
| 214 | 214 | | exit |
| 215 | 215 | | #end |