-
Notifications
You must be signed in to change notification settings - Fork 11
/
ucs-service-ctl.sh
executable file
·108 lines (100 loc) · 2.45 KB
/
ucs-service-ctl.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
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
#!/bin/bash
. /lib/lsb/init-functions
WORKINGPATH=`pwd`
SUPERVISORD=/usr/local/bin/supervisord
SUPERVISORCTL=/usr/local/bin/supervisorctl
NAME=supervisord
DESC=supervisor
LOGDIR=/var/log/supervisor
PIDFILE=/var/run/$NAME.pid
DODTIME=5
OPTS="-c $WORKINGPATH/supervisord.conf"
USER=`whoami`
export WORKINGPATH
export LOGDIR
export USER
[ -f $SUPERVISORD ] || exit 0
[ -d "$LOGDIR" ] || mkdir "$LOGDIR"
set -e
check_running_core() {
pid=$1
name=$2
[ -z "$pid" ] && return 1
[ ! -d /proc/$pid ] && return 1
(cat /proc/$pid/cmdline | tr "\000" "\n"|grep -q $name) || return 1
return 0
}
check_running() {
[ ! -f "$PIDFILE" ] && return 1
pid=`cat $PIDFILE`
check_running_core $pid $SUPERVISORD || return 1
return 0
}
force_stop() {
[ ! -f "$PIDFILE" ] && return
if check_running ;then
kill -15 $pid
# Is it really dead?
[ -n "$DODTIME" ] && sleep "$DODTIME"s
if check_running ;then
kill -9 $pid
[ -n "$DODTIME" ] && sleep "$DODTIME"s
if check_running ;then
echo "Cannot kill $NAME (pid=$pid)!"
exit 1
fi
fi
fi
rm -f $PIDFILE
return 0
}
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile $PIDFILE \
--exec $SUPERVISORD -- $OPTS
sleep 1 && [ -f $PIDFILE ] || sleep 1
if check_running ;then
echo "$NAME."
else
echo "ERROR."
fi
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
echo "$NAME."
;;
force-stop)
echo -n "Forcefully stopping $DESC: "
force_stop
if ! check_running ;then
echo "$NAME."
else
echo "ERROR."
fi
;;
restart)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
[ -n "$DODTIME" ] && sleep $DODTIME
start-stop-daemon --start --quiet --pidfile $PIDFILE \
--exec $SUPERVISORD -- $OPTS
echo "$NAME."
;;
status)
echo -n "$NAME is "
if check_running ; then
echo "running"
else
echo "not running."
exit 1
fi
$SUPERVISORCTL status
;;
*)
echo "Usage: sudo ./ucs-service-ctl.sh {start|stop|restart|status}" >&2
exit 1
;;
esac
exit 0