-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmonitorWatchdog.sh
More file actions
executable file
·89 lines (78 loc) · 1.51 KB
/
Copy pathXmonitorWatchdog.sh
File metadata and controls
executable file
·89 lines (78 loc) · 1.51 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash
declare -A mapper
conf=dog.pid
function debug
{
echo $@
}
function watch
{
local pid=$1
local index=`ps -ef|awk '{print $2}'|grep -P "^${pid}$"`
if [ "${index}None" = "None" ]; then
echo gone
return
fi
echo stay
}
function dogit
{
while [ 1 ]
do
sleep 20
for pid in ${!mapper[@]}
do
# debug pid:$pid
local t=`watch $pid`
# debug "test result is $t!!!"
if [ "$t" = "gone" ]; then
# debug "$c with pid $pid was gone"
loadscript ${mapper[$pid]}
unset mapper[$pid]
sed -i "/$pid/d" $conf
fi
done
done
}
function loadscript
{
local script=$@
# debug script=$script
$script > /dev/null 2>&1 &
local pid=$!
mapper[$pid]=$script
echo $pid >> $conf
}
function clean
{
if [ -f $conf ]; then
while read line; do
if [ -n "$line" ]; then
pid=`ps -ef|awk '{print $2}'|grep $line|grep -v 'grep'`
if [ -n "${pid}" ]; then
# debug killing $pid
kill $pid
fi
fi
done < $conf
fi
echo > $conf
# debug done!
}
function main
{
clean
local file=$1
# echo $file
if [ -f $file ]; then
while read line
do
loadscript $line
done < $file
dogit
else
echo "Not a file!"
fi
}
cd $(dirname $0)
main $1;