-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkira.sh
executable file
·59 lines (47 loc) · 1.23 KB
/
kira.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
#!/bin/bash
if [ -z $2 ]; then
echo "Usage: $0 <process-regex> <program-name>"
echo "Example: $0 /usr/local/bin/uri2png uri2png"
exit
fi
PROCESS_REGEX=$1
PROGRAM_NAME=$2
KIRA_PID=$$
LOG_FILE=/tmp/kira-$PROGRAM_NAME.log
PIDS_FILE=/tmp/kira-$PROGRAM_NAME.pids
touch $PIDS_FILE
function emit_log {
ms=$(($(date +%s%N)/1000000))
echo "$ms $1" >> $LOG_FILE
}
function kira_pids {
# kill pids if they have been hanging around for too long.
for pid in `pgrep -f $PROCESS_REGEX`
do
# if pid is listed in the pids_file, kill it.
if grep -q $pid $PIDS_FILE
then
ps_of_pid=`ps -f --no-headers --pid $pid`
emit_log "killing pid $pid | $ps_of_pid"
kill $pid
fi
done
# clear the pid file.
> $PIDS_FILE
}
function watch_pids {
# collect pids of the given program, save them to $PIDSFILE.
for pid in `pgrep -f $PROCESS_REGEX`
do
# don't match kira program (self).
if [ "$pid" != "$KIRA_PID" ]
then
emit_log "watching pid $pid"
echo $pid >> $PIDS_FILE
fi
done
}
# call our kira_pids (killer) function.
kira_pids
# call our watch_pids function.
watch_pids