Debian Codegraph @

debian/groonga-server-gqtp.init

raw
1 #! /bin/sh
2 ### BEGIN INIT INFO
3 # Provides: groonga-server-gqtp
4 # Required-Start: $remote_fs $network
5 # Required-Stop: $remote_fs $network
6 # Default-Start: 2 3 4 5
7 # Default-Stop: 0 1 6
8 # Short-Description: groonga's init script
9 # Description: groonga is a full-text search engine and column store.
10 ### END INIT INFO
11
12 # Author: Kouhei Sutou <[email protected]>
13
14 # Do NOT "set -e"
15
16 # PATH should only include /usr/* if it runs after the mountnfs.sh script
17 PATH=/sbin:/usr/sbin:/bin:/usr/bin
18 DESC="a full-text search engine and column store"
19 NAME=groonga
20 DAEMON=/usr/bin/$NAME
21 USER=_groonga
22 GROUP=groonga
23 DATABASE=/var/lib/groonga/db/db
24 ADDRESS=127.0.0.1
25 PORT=10043
26 PROTOCOL=gqtp
27 LOG_PATH=/var/log/groonga/groonga-$PROTOCOL.log
28 QUERY_LOG_PATH=/var/log/groonga/query-$PROTOCOL.log
29 PIDFILE=/var/run/groonga/$NAME-$PROTOCOL.pid
30 SCRIPTNAME=/etc/init.d/$NAME-server-$PROTOCOL
31 OPTION_ARGS=""
32 START_STOP_DAEMON_ARGS=""
33 ENABLE="no"
34
35 DEFAULT_FILE=/etc/default/groonga-server-gqtp
36
37 # Exit if the package is not installed
38 [ -x "$DAEMON" ] || exit 0
39
40 # Read configuration variable file if it is present
41 [ -r $DEFAULT_FILE ] && . $DEFAULT_FILE
42
43 [ "$ENABLE" = "yes" ] || exit 0
44
45 DAEMON_ARGS="-d --pid-path ${PIDFILE}"
46 if [ -n "${ADDRESS}" ]; then
47 DAEMON_ARGS="${DAEMON_ARGS} --bind-address ${ADDRESS}"
48 fi
49 if [ -n "${PORT}" ]; then
50 DAEMON_ARGS="${DAEMON_ARGS} --port ${PORT}"
51 fi
52 if [ -n "${LOG_PATH}" ]; then
53 DAEMON_ARGS="${DAEMON_ARGS} --log-path ${LOG_PATH}"
54 fi
55 if [ -n "${QUERY_LOG_PATH}" ]; then
56 DAEMON_ARGS="${DAEMON_ARGS} --query-log-path ${QUERY_LOG_PATH}"
57 fi
58 if [ -n "${PROTOCOL}" ]; then
59 DAEMON_ARGS="${DAEMON_ARGS} --protocol ${PROTOCOL}"
60 fi
61 mkdir -p $(dirname ${PIDFILE})
62 if [ -n "${USER}" ]; then
63 if ! getent passwd | grep -q "^${USER}:"; then
64 echo "$0: user for running groonga doesn't exist: ${USER}" >&2
65 exit 1
66 fi
67 chown -R ${USER} $(dirname ${PIDFILE})
68 START_STOP_DAEMON_ARGS="${START_STOP_DAEMON_ARGS} --chuid ${USER}"
69 fi
70 if [ -n "${GROUP}" ]; then
71 if ! getent group | grep -q "^${GROUP}:"; then
72 echo "$0: group for running groonga doesn't exist: ${GROUP}" >&2
73 exit 1
74 fi
75 START_STOP_DAEMON_ARGS="${START_STOP_DAEMON_ARGS} --group ${GROUP}"
76 fi
77 DAEMON_ARGS="${DAEMON_ARGS} ${OPTION_ARGS}"
78
79 if [ -z "${DATABASE}" ]; then
80 echo "$0: DATABASE should not be empty" >&2
81 exit 1
82 fi
83
84 if [ -f "${DATABASE}" ]; then
85 DAEMON_ARGS="${DAEMON_ARGS} ${DATABASE}"
86 else
87 mkdir -p $(dirname ${DATABASE})
88 if [ -n "${USER}" ]; then
89 chown -R ${USER} $(dirname ${DATABASE})
90 fi
91 if [ -n "${GROUP}" ]; then
92 chgrp -R ${GROUP} $(dirname ${DATABASE})
93 fi
94 DAEMON_ARGS="${DAEMON_ARGS} -n ${DATABASE}"
95 fi
96
97 # Load the VERBOSE setting and other rcS variables
98 . /lib/init/vars.sh
99
100 # Define LSB log_* functions.
101 # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
102 . /lib/lsb/init-functions
103
104 send_command()
105 {
106 command=$1
107 $DAEMON --port ${PORT} -c ${ADDRESS} ${command}
108 }
109
110 #
111 # Function that starts the daemon/service
112 #
113 do_start()
114 {
115 # Return
116 # 0 if daemon has been started
117 # 1 if daemon was already running
118 # 2 if daemon could not be started
119 start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON \
120 ${START_STOP_DAEMON_ARGS} --test > /dev/null || return 1
121 start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON \
122 ${START_STOP_DAEMON_ARGS} -- $DAEMON_ARGS || return 2
123 # Add code here, if necessary, that waits for the process to be ready
124 # to handle requests from services started subsequently which depend
125 # on this one. As a last resort, sleep for some time.
126 }
127
128 #
129 # Function that stops the daemon/service
130 #
131 do_stop()
132 {
133 # Return
134 # 0 if daemon has been stopped
135 # 1 if daemon was already stopped
136 # 2 if daemon could not be stopped
137 # other if a failure occurred
138
139 send_command shutdown
140
141 start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 \
142 --pidfile $PIDFILE --name $NAME
143 RETVAL="$?"
144 [ "$RETVAL" = 2 ] && return 2
145 # Wait for children to finish too if this is a daemon that forks
146 # and if the daemon is only ever run from this initscript.
147 # If the above conditions are not satisfied then add some other code
148 # that waits for the process to drop all resources that could be
149 # needed by services started subsequently. A last resort is to
150 # sleep for some time.
151 start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 \
152 --pidfile $PIDFILE --exec $DAEMON
153 [ "$?" = 2 ] && return 2
154 # Many daemons don't delete their pidfiles when they exit.
155 rm -f $PIDFILE
156 return "$RETVAL"
157 }
158
159 #
160 # Function that sends a SIGHUP to the daemon/service
161 #
162 do_reload() {
163 #
164 # If the daemon can reload its configuration without
165 # restarting (for example, when it is sent a SIGHUP),
166 # then implement that here.
167 #
168 start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
169 return 0
170 }
171
172 do_status() {
173 if [ -z "${ADDRESS}" ]; then
174 ADDRESS="localhost"
175 fi
176 send_command status
177 return $?
178 }
179
180 case "$1" in
181 start)
182 [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
183 do_start
184 case "$?" in
185 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
186 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
187 esac
188 ;;
189 stop)
190 [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
191 do_stop
192 case "$?" in
193 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
194 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
195 esac
196 ;;
197 reload|force-reload)
198 log_daemon_msg "Reloading $DESC" "$NAME"
199 do_reload
200 log_end_msg $?
201 ;;
202 restart)
203 log_daemon_msg "Restarting $DESC" "$NAME"
204 do_stop
205 case "$?" in
206 0|1)
207 do_start
208 case "$?" in
209 0) log_end_msg 0 ;;
210 1) log_end_msg 1 ;; # Old process is still running
211 *) log_end_msg 1 ;; # Failed to start
212 esac
213 ;;
214 *)
215 # Failed to stop
216 log_end_msg 1
217 ;;
218 esac
219 ;;
220 status)
221 do_status
222 case "$?" in
223 0) ;; # service is OK
224 *) exit 3 ;; # program is not running
225 esac
226 ;;
227 *)
228 echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload|status}" >&2
229 exit 3
230 ;;
231 esac
232
233 :