Skip to content

Commit 46fe23f

Browse files
committed
added init.d scripts to start gunicorn and pyscada daemons
1 parent 809dd43 commit 46fe23f

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

Diff for: gunicorn_django

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/bin/bash
2+
3+
### BEGIN INIT INFO
4+
# Provides: gunicorn_django
5+
# Required-Start: $all
6+
# Required-Stop: $all
7+
# Default-Start: 2 3 4 5
8+
# Default-Stop: 0 1 6
9+
# Short-Description: starts the pyscada gunicorn server
10+
# Description: starts pyscada gunicorn using start-stop-daemon
11+
### END INIT INFO
12+
13+
# sample configfile for /etc/default/gunicorn_django
14+
# SERVERS=(
15+
# 'APP_NAME DJANGODIR number_of_workers'
16+
# )
17+
# RUN_AS='www-data'
18+
19+
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
20+
if [ -f /etc/default/gunicorn_django ] ; then
21+
. /etc/default/gunicorn_django
22+
fi
23+
24+
25+
26+
start () {
27+
28+
for i in "${SERVERS[@]}"
29+
do
30+
:
31+
set -- "$i"
32+
IFS=" "; declare -a data=($*)
33+
34+
if [ "$SERVER" ]; then
35+
if [ "$SERVER" != ${data[0]} ]; then
36+
continue
37+
fi
38+
fi
39+
if [ -e ${data[1]}/run/gunicorn.pid ]
40+
then
41+
PID=$(cat ${data[1]}/run/gunicorn.pid)
42+
if ps -p $PID > /dev/null
43+
then
44+
echo "gunicorn service is already running ($PID)"
45+
exit 0
46+
fi
47+
fi
48+
49+
echo "Spawning ${data[0]}"
50+
# Create the run directory if it doesn't exist
51+
RUNDIR=$(dirname ${data[1]}/run/gunicorn.sock)
52+
test -d $RUNDIR || mkdir -p $RUNDIR
53+
export DJANGO_SETTINGS_MODULE=${data[0]}.settings
54+
export PYTHONPATH=${data[1]}:$PYTHONPATH
55+
# Start your Django Unicorn
56+
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
57+
start-stop-daemon --start --quiet -c $RUN_AS -d ${data[1]} --pidfile ${data[1]}/run/gunicorn.pid --exec /usr/local/bin/gunicorn -- ${data[0]}.wsgi:application -n ${data[0]} -w ${data[2]} -u $RUN_AS -b unix:${data[1]}/run/gunicorn.sock -p ${data[1]}/run/gunicorn.pid -D
58+
59+
done
60+
return
61+
}
62+
63+
stop () {
64+
for i in "${SERVERS[@]}"
65+
do
66+
:
67+
set -- "$i"
68+
IFS=" "; declare -a data=($*)
69+
if [ "$SERVER" ]; then
70+
if [ "$SERVER" != ${data[0]} ]; then
71+
continue
72+
fi
73+
fi
74+
if [ -e ${data[1]}/run/gunicorn.pid ]
75+
then
76+
kill -TERM $(cat ${data[1]}/run/gunicorn.pid)
77+
echo "stopped service"
78+
else
79+
echo "service not running"
80+
fi
81+
done
82+
}
83+
84+
case "$1" in
85+
start)
86+
echo "Starting"
87+
start
88+
;;
89+
stop)
90+
echo "Stopping"
91+
stop
92+
;;
93+
restart)
94+
echo "Restarting"
95+
stop
96+
sleep 1
97+
start
98+
;;
99+
*)
100+
N=/etc/init.d/$NAME
101+
echo "Usage: $N {start|stop|restart} " >&2
102+
exit 1
103+
;;
104+
esac
105+
106+
exit 0

Diff for: pyscada_daemon

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/bin/bash
2+
3+
### BEGIN INIT INFO
4+
# Provides: pyscada_daemon
5+
# Required-Start: $all
6+
# Required-Stop: $all
7+
# Default-Start: 2 3 4 5
8+
# Default-Stop: 0 1 6
9+
# Short-Description: starts the pyscada daemon
10+
# Description: starts pyscada daemon
11+
### END INIT INFO
12+
13+
# sample configfile for /etc/default/pyscada_daemon
14+
#####################################################
15+
# DAEMONS=(
16+
# 'NAME DJANGODIR'
17+
#)
18+
# RUN_AS='www-data'
19+
20+
if [ -f /etc/default/pyscada_daemon ] ; then
21+
. /etc/default/pyscada_daemon
22+
fi
23+
24+
25+
26+
start () {
27+
for i in "${DAEMONS[@]}"
28+
do
29+
:
30+
set -- "$i"
31+
IFS=" "; declare -a data=($*)
32+
33+
34+
echo "Spawning ${data[1]} ${data[0]} daemon"
35+
start-stop-daemon --start -c $RUN_AS -d ${data[1]} --exec manage.py -- PyScadaDaemonHandler ${data[0]} start
36+
37+
done
38+
return
39+
}
40+
41+
42+
stop () {
43+
for i in "${DAEMONS[@]}"
44+
do
45+
:
46+
set -- "$i"
47+
IFS=" "; declare -a data=($*)
48+
start-stop-daemon --start -c $RUN_AS -d ${data[1]} --exec manage.py -- PyScadaDaemonHandler ${data[0]} stop
49+
done
50+
return
51+
}
52+
53+
case "$1" in
54+
start)
55+
echo "Starting"
56+
start
57+
;;
58+
stop)
59+
echo "Stopping"
60+
stop
61+
;;
62+
restart)
63+
echo "Restarting"
64+
stop
65+
sleep 1
66+
start
67+
;;
68+
*)
69+
N=/etc/init.d/$NAME
70+
echo "Usage: $N {start|stop|restart} " >&2
71+
exit 1
72+
;;
73+
esac
74+
75+
exit 0

0 commit comments

Comments
 (0)