-
Notifications
You must be signed in to change notification settings - Fork 0
/
envreact
executable file
·63 lines (51 loc) · 1.31 KB
/
envreact
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
#!/bin/sh
#
# envreact
usage() {
base=$(basename "$0")
cat >&2 << EOF
Usage:
$base
$autoreact must exist and be populated.
EOF
[ $# -eq 0 ] || exit "$1"
}
react() {
while read -r line; do
prgs="${line#* }" cmd="${line%% *}" bin="${cmd%_*}" opt="${cmd#*_}"
# test strings to see if program is running
for prg in $prgs; do
pgrep -f "$prg" > /dev/null && prg_alive=1
done
case "$prg_alive" in
1)
case "$opt" in
run) pgrep "$bin" > /dev/null || "$bin" & ;;
end) pkill "$bin" 2> /dev/null ;;
oneshot)
for prg in $prgs; do
$prg
done
;;
esac
;;
*)
case "$opt" in
end) pgrep "$bin" > /dev/null || "$bin" & ;;
run) pkill "$bin" 2> /dev/null ;;
oneshot) continue ;;
esac
;;
esac
unset prg_alive
done < "$autoreact"
}
main() {
autoreact="$HOME/.autoreact"
[ -f "$autoreact" ] || usage 1
while :; do
react
sleep 1
done
}
main "$@"