-
Notifications
You must be signed in to change notification settings - Fork 2
/
ecagentd
executable file
·124 lines (103 loc) · 2.61 KB
/
ecagentd
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#! /bin/sh
### BEGIN INIT INFO
# Provides: ecagentd
# Required-Start: $remote_fs $network
# Required-Stop: $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start ECM Agent daemon
### END INIT INFO
set +e
NAME=ecagentd
TWISTD=/usr/bin/twistd
TAC=$NAME.tac
DESC="ECM Agent"
test -x $TWISTD || exit 0
DIR=/opt/ecmanaged/ecagent
PID_FILE=$DIR/twistd.pid
# Check working dir
if test ! -d "$DIR"; then
echo "Unable to access work dir: $DIR"
exit 1;
fi
export LANG="C"
export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
# clean APT related environment when started from cloud-init
# bug: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=439763
unset PERL_DL_NONLAZY
unset DEBCONF_REDIR
unset DEBIAN_FRONTEND
unset DEBIAN_HAS_FRONTEND
unset DPKG_NO_TSTP
# define LSB log_* functions.
. /lib/lsb/init-functions
getpid() {
echo `cat $PID_FILE 2>/dev/null`
}
killagent() {
PID=$(getpid)
ls -l /proc/$PID/exe > /dev/null 2>&1
case "$?" in
0) kill -9 $PID >/dev/null 2>&1; pkill -f ${TAC} >/dev/null 2>&1 ;;
*) pkill -f ${TAC} >/dev/null 2>&1 ;;
esac
}
zombies() {
PID=$(getpid)
for i in $(ps axo ppid,state,etimes|grep -w Z|awk '{if ($3 >= 300) print $1 }'); do
if [ $i -eq ${PID} ]; then
$0 restart
fi
done
}
case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
killagent
/bin/rm -f $PID_FILE > /dev/null 2>&1
start-stop-daemon --oknodo --start --user ecmanaged --pidfile $PID_FILE \
--background --chdir $DIR --chuid ecmanaged--exec $TWISTD -- -ny $TAC >/dev/null 2>&1
case "$?" in
0) log_end_msg 0 ;;
*) log_end_msg 1; exit 1 ;;
esac
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
start-stop-daemon --retry 5 --oknodo --stop --user ecmanaged --signal 9 --quiet --pidfile $PID_FILE 2>/dev/null
case "$?" in
0) log_end_msg 0; killagent ;;
*) log_end_msg 1; killagent ; exit 1 ;;
esac
;;
status)
log_daemon_msg "Checking status of $DESC" "$NAME"
PID=$(getpid)
ls -l /proc/$PID/exe > /dev/null 2>&1
case "$?" in
0) log_end_msg 0 ;;
*) log_end_msg 1; exit 1 ;;
esac
;;
check)
log_daemon_msg "Checking status of $DESC" "$NAME"
PID=$(getpid)
ls -l /proc/$PID/exe > /dev/null 2>&1
case "$?" in
0) zombies; log_end_msg 0 ;;
*) $0 restart ;;
esac
;;
kill)
killagent
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: /etc/init.d/$NAME {start|stop|restart|status|check}" >&2
exit 1
;;
esac
exit 0