-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer
executable file
·43 lines (36 loc) · 1.07 KB
/
timer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash
TIME=0
while [ -n "$1" ]; do
ARG="$1"
COUNT=$(echo "$ARG" | grep -o '[0-9]\+')
case "$ARG" in
*s) TIME=$((TIME+COUNT));;
*m) TIME=$((TIME+60*COUNT));;
*h) TIME=$((TIME+60*60*COUNT));;
*d) echo "I recommend a cronjob."; exit 1;;
*) break;
esac
shift
done
if [ 0 -eq "$TIME" ]; then
echo "I'm sure you didn't mean to set a timer zero seconds from now." >&2
exit 1
fi
FINISH_SECONDS=$(($(date +%s)+TIME))
FINISH_TIME=$(date --date=@$FINISH_SECONDS)
MESSAGE="Timer $$ done."
DONE_CMD=( feedback -u critical "$MESSAGE" )
case "$1" in
-r) shift; DONE_CMD=( raction poke "$@" );;
-c) shift; DONE_CMD=( "$@" );;
'') ;; # so we don't make MESSAGE empty with no args
*) MESSAGE="$*";;
esac
( (
(sleep $TIME; exit 1) &
TIMERPID=$!
trap "echo $TIMERPID $(($FINISH_SECONDS - $(date +%s))) $FINISH_TIME >> /tmp/timers.state" SIGUSR1
echo "Timer set for $TIME seconds from now ($FINISH_TIME). PID=$TIMERPID";
wait $TIMEPID && "${DONE_CMD[@]}";
echo "Timer $TIMERPID done." >&2
)& )&