SysAdmin/Pootle: rqworker

File rqworker, 3.9 KB (added by Fran Boon, 5 years ago)

init.d

Line 
1#!/bin/sh
2### BEGIN INIT INFO
3# Provides: rqworker
4# Required-Start: $local_fs $remote_fs $network
5# Required-Stop: $local_fs $remote_fs $network
6# Default-Start: 2 3 4 5
7# Default-Stop: 0 1 6
8# Short-Description: Start/stop custom rqworker instance
9### END INIT INFO
10
11# Author: Leonid Borisenko <leo.borisenko@gmail.com>
12# modified for Pootle by: Fran Boon <fran@aidiq.com>
13
14# PATH should only include /usr/* if it runs after the mountnfs.sh script
15PATH=/sbin:/usr/sbin:/bin:/usr/bin
16
17SCRIPTNAME=/etc/init.d/rqworker
18
19DESC="Pootle rqworker" # Introduce a short description here
20DAEMON=/home/pootle/env/bin/pootle # Introduce the server's location here
21#DAEMON=/home/pootle/env/bin/python # Introduce the server's location here
22
23UID=pootle # Introduce uid here
24GID=pootle # Introduve gid here
25
26RUNDIR=/run/rqworker
27
28PIDFILE=$RUNDIR/pid
29
30DAEMON_ARGS=" \
31 rqworker \
32"
33
34# Exit if the package is not installed
35[ -x $DAEMON ] || exit 0
36
37# Load the VERBOSE setting and other rcS variables
38. /lib/init/vars.sh
39
40# Define LSB log_* functions.
41# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
42. /lib/lsb/init-functions
43
44# Return:
45# 0 if daemon has been started
46# 1 if daemon was already running
47# 2 if daemon could not be started
48do_start()
49{
50 install -d -o $UID -g $GID -m 755 $RUNDIR
51
52 cd /home/pootle
53 . env/bin/activate
54 export PYTHONPATH=.:/home/pootle/env/lib/python2.7/site-packages/:/home/pootle/env/lib/python2.7/site-packages/pootle/apps/
55 #exec start-stop-daemon --start --chuid pootle --exec $DAEMON /home/pootle/env/bin/pootle rqworker -- >> /home/pootle/rqworker.log 2>&1 &
56 exec start-stop-daemon --start --chuid pootle --exec $DAEMON rqworker -- >> /home/pootle/rqworker.log 2>&1 &
57
58 local INTERVAL_START=$(date +%s)
59 local INTERVAL_END=$(date +%s)
60 local WAITING=2 # seconds
61
62 # Wait until daemon getting to create pidfile.
63 while [ ! -e "$PIDFILE" ]; do
64 INTERVAL_END=$(date +%s)
65 if [ $(expr $INTERVAL_END - $INTERVAL_START) -gt $WAITING ]; then
66 return
67 fi
68 sleep 0.05
69 done
70
71 chown root:root $PIDFILE
72 chmod 644 $PIDFILE
73
74 return 0
75}
76
77# Return:
78# 0 if daemon has been stopped
79# 1 if daemon was already stopped
80# 2 if daemon could not be stopped
81# other if a failure occurred
82do_stop()
83{
84 start-stop-daemon --stop --quiet \
85 --retry=QUIT/30/KILL/5 \
86 --pidfile $PIDFILE \
87 --exec $DAEMON
88
89 RETVAL="$?"
90 [ "$RETVAL" = 2 ] && return 2
91
92 rm -rf $RUNDIR
93
94 return "$RETVAL"
95}
96
97# Return:
98# 0 if daemon has been reloaded
99# 3 if daemon could not be reloaded
100do_reload()
101{
102 start-stop-daemon --stop --quiet \
103 --signal=HUP \
104 --pidfile $PIDFILE \
105 --exec $DAEMON
106
107 RETVAL="$?"
108
109 # There is no such process, nothing to reload!
110 [ "$RETVAL" = 1 ] && RETVAL=3
111
112 return "$RETVAL"
113}
114
115# Return:
116# 0 if daemon has been reloaded
117# 3 if daemon could not be reloaded
118do_force_reload()
119{
120 start-stop-daemon --stop --quiet \
121 --signal=TERM \
122 --pidfile $PIDFILE \
123 --exec $DAEMON
124
125 RETVAL="$?"
126
127 # There is no such process, nothing to reload!
128 [ "$RETVAL" = 1 ] && RETVAL=3
129
130 return "$RETVAL"
131}
132
133case "$1" in
134 start)
135 log_daemon_msg "Starting $DESC" "$NAME"
136 do_start
137 log_end_msg "$?"
138 ;;
139
140 stop)
141 log_daemon_msg "Stopping $DESC" "$NAME"
142 do_stop
143 log_end_msg "$?"
144 ;;
145
146 status)
147 status_of_proc -p "$PIDFILE" "$DAEMON" "$NAME" \
148 && exit 0 \
149 || exit $?
150 ;;
151
152 reload)
153 log_daemon_msg "Reloading $DESC" "$NAME"
154 do_reload
155 log_end_msg "$?"
156 ;;
157
158 force-reload)
159 log_daemon_msg "Forced reloading $DESC" "$NAME"
160 do_force_reload
161 log_end_msg "$RETVAL"
162 ;;
163
164 restart)
165 log_daemon_msg "Restarting $DESC" "$NAME"
166 do_stop
167 case "$?" in
168 0)
169 do_start
170 log_end_msg "$?"
171 ;;
172 *)
173 # Failed to stop
174 log_end_msg 1
175 ;;
176 esac
177 ;;
178
179 *)
180 echo "Usage: $SCRIPTNAME {start|stop|status|restart|reload|force-reload}" >&2
181 exit 3
182 ;;
183esac
184
185: