-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathntrm.sh
executable file
·66 lines (61 loc) · 1.82 KB
/
ntrm.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/sh
[ "$#" -eq 0 ] && { echo "Usage: ntrm -a|-A|-l|-n|<id>"; exit 2 ;}
remove_by_id() {
pidfile="$(
at -c "$1" 2>/dev/null | awk -F'>' '
/^NT_MESSAGE=/ {e=1}
$1=="echo \"$$\" " {print $2; exit}
END {exit !e}
'
)" || { echo "ntrm: invalid id" >&2; exit 1 ;}
if at -l | awk -v id="$1" '$1==id && $7=="=" {e=1; exit}; END {exit !e}' ; then
read -r PID 2>/dev/null <"$pidfile" && kill "$PID" $(pgrep -P "$PID")
else
at -r "$1"
fi
rm -f "$pidfile"
}
remove_by_job() {
id="${1#?}"
pidfile="$(
at -c "$id" | awk -F'>' '
/^NT_MESSAGE=/ {e=1}
$1=="echo \"$$\" " {print $2; exit}
END {exit !e}
'
)" || return 1
case "$1" in
=*) read -r PID 2>/dev/null <"$pidfile" && kill "$PID" $(pgrep -P "$PID") ;;
*) at -r "$id" ;;
esac
rm -f "$pidfile"
return 0
}
case "$1" in
-[aA])
if [ "$1" = -a ] ; then
printf "Remove all pending notification alarms? [y/N]: "
read -r confirm
[ "$confirm" != y ] && [ "$confirm" != Y ] && exit 0
fi
at -l | LC_ALL=C sort -k1,1 -k7,7 | awk 'BEGIN {id=-1}; id!=$1 {print $7$1; id=$1}' |
while IFS='' read -r job ; do
remove_by_job "$job"
done
;;
-l)
at -l | LC_ALL=C sort -k1,1r -k7,7 | awk 'BEGIN {id=-1}; id!=$1 {print $7$1; id=$1}' |
while IFS='' read -r job ; do
remove_by_job "$job" && exit 0
done
;;
-n)
at -l | LC_ALL=C sort -k6,6 -k3,3M -k4,5 | awk 'BEGIN {id=-1}; id!=$1 {print $7$1; id=$1}' |
while IFS='' read -r job ; do
remove_by_job "$job" && exit 0
done
;;
*)
remove_by_id "$1"
;;
esac