forked from agustim/cDistro
-
Notifications
You must be signed in to change notification settings - Fork 9
/
cdistro
140 lines (117 loc) · 4.74 KB
/
cdistro
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/sh
#
### BEGIN INIT INFO
# Provides: cDistro
# Required-Start: $local_fs $remote_fs $network $syslog $named
# Required-Stop: $local_fs $remote_fs $network $syslog $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive: true
# Short-Description: Start/Stop web configuration server
### END INIT INFO
NAME=cdistro
CONFFILE="/etc/cloudy/cloudy.conf"
PIDFILE="/var/run/cdistro.pid"
PIDDAEMON="/var/run/cdistrod.pid"
DAEMON="/usr/sbin/cdistrod"
DEMO=0
test -f $CONFFILE && . $CONFFILE || exit 0
start() {
STARTPHP=true
STARTPHPSUCCESS=false
STARTCDISTRO=true
STARTCDISTROSUCCESS=false
# First, check for a previously started PHP server
if [ -f "$PIDFILE" ]; then
# Server has been started previously. Check if it is still running
if $(netstat -ant|grep ${BINDIP}|grep ${PORT}|grep -q LISTEN); then
echo "cDistro PHP server is already running."
STARTPHP=false
else
echo "cDistro PHP server was already started but crashed. Restarting it..."
fi
fi
if [ $STARTPHP = true ]; then
echo "Starting cDistro PHP server..."
if [ $DEMO -eq 1 ]; then
echo "cDistro is in demo mode."
su - nobody -c "$SERVER $OPTS -S $BINDIP:$PORT -t $DOCROOT >/dev/null 2>&1 &" > /dev/null 2>&1
sleep 5
echo $(pidof php) > $PIDFILE
else
$SERVER $OPTS -S $BINDIP:$PORT -t $DOCROOT >/dev/null 2>&1 &
echo $! > $PIDFILE
fi
# Check if the daemon has been started properly (wait up to 5 seconds)
for i in $(seq 1 5); do
sleep 1
[ -f $PIDFILE ] && $(netstat -ant|grep ${BINDIP}|grep ${PORT}|grep -q LISTEN) \
&& kill -0 $(cat $PIDFILE) >/dev/null 2>&1 && echo "cDistro PHP server successfully started." \
&& STARTPHPSUCCESS=true && break
done
fi
$STARTPHP && ! $STARTPHPSUCCESS && echo "cDistro PHP server did not start successfully." && exit 1
# First, check for a previously started cDistro daemon
if [ -f "$PIDDAEMON" ]; then
# Server has been started previously. Check if it is still running
if kill -0 $(cat $PIDDAEMON) >/dev/null 2>&1; then
echo "cDistro daemon is already running."
STARTCDISTRO=false
else
echo "cDistro daemon was already started but crashed. Restarting it..."
fi
fi
if [ $STARTCDISTRO = true ]; then
echo "Starting cDistro daemon..."
$DAEMON >/dev/null 2>&1 &
echo $! > $PIDDAEMON
# Check if the daemon has been started properly (wait up to 5 seconds)
for i in $(seq 1 5); do
sleep 1
[ -f $PIDDAEMON ] && kill -0 $(cat $PIDDAEMON) >/dev/null 2>&1 \
&& echo "cDistro daemon server successfully started." \
&& STARTCDISTROSUCCESS=true && break
done
fi
$STARTCDISTRO && ! $STARTCDISTROSUCCESS && echo "cDistro daemon server did not start successfully." && exit 1
}
stop() {
# Stop cDistro PHP server
[ -f $PIDFILE ] && $(netstat -ant|grep ${BINDIP}|grep ${PORT}|grep -q LISTEN) && \
echo "Stopping cDistro PHP server..." && echo "Sending terminate signal to process $(cat $PIDFILE)..." && kill -15 $(cat $PIDFILE) && sleep 1
[ -f $PIDFILE ] && $(netstat -ant|grep ${BINDIP}|grep ${PORT}|grep -q LISTEN) && \
echo "Sending interrupt signal to process $(cat $PIDFILE)..." && kill -2 $(cat $PIDFILE) && sleep 1
[ -f $PIDFILE ] && $(netstat -ant|grep ${BINDIP}|grep ${PORT}|grep -q LISTEN) && \
echo "Sending kill signal to process $(cat $PIDFILE)..." && kill -9 $(cat $PIDFILE) && sleep 1
! $(netstat -ant|grep ${BINDIP}|grep ${PORT}|grep -q LISTEN) && [ -f $PIDFILE ] && rm $PIDFILE
# Stop cDistro daemon
[ -f $PIDDAEMON ] && kill -0 $(cat $PIDDAEMON) >/dev/null 2>&1 && echo "Stopping cDistro daemon..." && echo "Sending terminate signal to process $(cat $PIDDAEMON)..." && kill -15 $(cat $PIDDAEMON) && sleep 1
[ -f $PIDDAEMON ] && kill -0 $(cat $PIDDAEMON) >/dev/null 2>&1 && echo "Sending interrupt signal to process $(cat $PIDDAEMON)..." && kill -2 $(cat $PIDDAEMON) && sleep 1
[ -f $PIDDAEMON ] && kill -0 $(cat $PIDDAEMON) >/dev/null 2>&1 && echo "Sending kill signal to process $(cat $PIDDAEMON)..." && kill -9 $(cat $PIDDAEMON) && sleep 1
[ -f $PIDDAEMON ] && ! kill -0 $(cat $PIDDAEMON) >/dev/null 2>&1 && [ -f $PIDDAEMON ] && rm $PIDDAEMON
# Check that everything is stopped
{ ! [ -f $PIDFILE ] && ! $(netstat -ant|grep ${BINDIP}|grep ${PORT}|grep -q LISTEN) \
&& ! { [ -f $PIDDAEMON ] && ! kill -0 $(cat $PIDDAEMON) >/dev/null 2>&1 ;} \
&& echo "cDistro daemon stopped." && return 0; } || { echo "cDistro daemon was not successfully stopped. Quitting..." && exit 1; }
}
restart() {
stop
sleep 1
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "Usage: /etc/init.d/$NAME {start|stop|restart}"
exit 1
;;
esac
exit 0