-
Notifications
You must be signed in to change notification settings - Fork 3
/
alarm.sh
executable file
·52 lines (43 loc) · 1.3 KB
/
alarm.sh
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
44
45
46
47
48
49
50
51
52
#!/bin/bash
# A simple alarm clock script
echo "What time should the alarm go off? (HH:MM)"
read target
# sleep interval is 15 minutes
snooze=`dc -e "15 60 *p"`
# convert wakeup time to seconds
target_h=`echo $target | awk -F: '{print $1}'`
target_m=`echo $target | awk -F: '{print $2}'`
target_s_t=`dc -e "$target_h 60 60 ** $target_m 60 *+p"`
# get current time and convert to seconds
clock=`date | awk '{print $4}'`
clock_h=`echo $clock | awk -F: '{print $1}'`
clock_m=`echo $clock | awk -F: '{print $2}'`
clock_s=`echo $clock | awk -F: '{print $3}'`
clock_s_t=`dc -e "$clock_h 60 60 ** $clock_m 60 * $clock_s ++p"`
# calculate difference in times, add number of sec. in day and mod by same
sec_until=`dc -e "24 60 60 **d $target_s_t $clock_s_t -+r%p"`
echo "The alarm will go off at $target."
sleep $sec_until
# snooze loop
while :
do
echo -e "\nWake up!"
./playlist_buzzer.sh &
bpid=$!
disown $bpid # eliminates termination message
read -n1 input
for bsub in $(ps -o pid,ppid -ax | \
awk "{ if (\$2 == $bpid) { print \$1 }}")
do
kill $bsub # kill children
done
kill $bpid
if [ "$input" == "Q" ]
then
echo -e "\nGood morning!"
exit
else
echo -e "\nSnoozing for $snooze seconds..."
sleep $snooze
fi
done