forked from jmscott/blobio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zap-proc
65 lines (54 loc) · 1.3 KB
/
zap-proc
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
#
# Synopsis:
# Chatty zapping of a background process using pkill -F <pid-file>
#
PROG=$(basename $0)
PROC_OWNER=$(id -nu)
log()
{
echo "$(date +'%Y/%m/%d %H:%M:%S'): $PROG: $@"
}
die()
{
log "ERROR: $@" >&2
exit 1
}
test $# = 1 || die 'wrong number of arguments'
PID_FILE=$1
PROC_NAME=$(basename $(basename $PID_FILE) .pid)
if [ ! -e $PID_FILE ]; then
log "no pid file (ok): $PID_FILE"
log "process $PROC_NAME probably not running"
exit 0
fi
log "pid file exists: $PID_FILE"
log "process owner: $PROC_OWNER"
PID=$(cat $PID_FILE)
STATUS=$?
test $? = 0 || die "cat $PID_FILE failed: exit status=$STATUS"
log "process to kill: $PROC_NAME#$PID owned by $PROC_OWNER"
pkill -TERM -F $PID_FILE -u $PROC_OWNER
STATUS=$?
case "$STATUS" in
0)
log "zapped $PROC_NAME process with pid #$PID"
log "sleeping 3 seconds while process #$PID exits ..."
sleep 3
;;
1)
log "no process matches with pid #$PID"
rm -f $PID_FILE || die "rm -f $PID_FILE failed: exit status=$?"
;;
*)
die "pkill TERM $PID failed; exit status=$?"
;;
esac
# make sure pid file got removed by clean exit.
# otherwise, nuke process with SIGKILL.
if [ -e $PID_FILE ]; then
log "WARN: $PID_FILE still exists"
log "nuking $PROC_NAME#$PID with sig KILL"
kill -9 $PID
rm -f $PID_FILE || die "rm -f $PID_FILE failed: exit status=$?"
fi
exit 0