|
1 #!/bin/sh |
|
2 # |
|
3 # Example init.d script with LSB support. |
|
4 # |
|
5 # Please read this init.d carefully and modify the sections to |
|
6 # adjust it to the program you want to run. |
|
7 # |
|
8 # Copyright (c) 2007 Javier Fernandez-Sanguino <jfs@debian.org> |
|
9 # |
|
10 # This is free software; you may redistribute it and/or modify |
|
11 # it under the terms of the GNU General Public License as |
|
12 # published by the Free Software Foundation; either version 2, |
|
13 # or (at your option) any later version. |
|
14 # |
|
15 # This is distributed in the hope that it will be useful, but |
|
16 # WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18 # GNU General Public License for more details. |
|
19 # |
|
20 # You should have received a copy of the GNU General Public License with |
|
21 # the Debian operating system, in /usr/share/common-licenses/GPL; if |
|
22 # not, write to the Free Software Foundation, Inc., 59 Temple Place, |
|
23 # Suite 330, Boston, MA 02111-1307 USA |
|
24 # |
|
25 ### BEGIN INIT INFO |
|
26 # Provides: ftbackup |
|
27 # Required-Start: $network $local_fs |
|
28 # Required-Stop: |
|
29 # Should-Start: $named |
|
30 # Should-Stop: |
|
31 # Default-Start: 2 3 4 5 |
|
32 # Default-Stop: 0 1 6 |
|
33 # Short-Description: <Enter a short description of the sortware> |
|
34 # Description: <Enter a long description of the software> |
|
35 # <...> |
|
36 # <...> |
|
37 ### END INIT INFO |
|
38 |
|
39 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin |
|
40 |
|
41 DAEMON=/usr/sbin/ftbackup # Introduce the server's location here |
|
42 NAME=#PACKAGE # Introduce the short server's name here |
|
43 DESC=#PACKAGE # Introduce a short description here |
|
44 LOGDIR=/var/log/ftbackup # Log directory to use |
|
45 |
|
46 PIDFILE=/var/run/$NAME.pid |
|
47 |
|
48 test -x $DAEMON || exit 0 |
|
49 |
|
50 . /lib/lsb/init-functions |
|
51 |
|
52 # Default options, these can be overriden by the information |
|
53 # at /etc/default/$NAME |
|
54 DAEMON_OPTS="" # Additional options given to the server |
|
55 |
|
56 DIETIME=10 # Time to wait for the server to die, in seconds |
|
57 # If this value is set too low you might not |
|
58 # let some servers to die gracefully and |
|
59 # 'restart' will not work |
|
60 |
|
61 #STARTTIME=2 # Time to wait for the server to start, in seconds |
|
62 # If this value is set each time the server is |
|
63 # started (on start or restart) the script will |
|
64 # stall to try to determine if it is running |
|
65 # If it is not set and the server takes time |
|
66 # to setup a pid file the log message might |
|
67 # be a false positive (says it did not start |
|
68 # when it actually did) |
|
69 |
|
70 LOGFILE=$LOGDIR/$NAME.log # Server logfile |
|
71 #DAEMONUSER=ftbackup # Users to run the daemons as. If this value |
|
72 # is set start-stop-daemon will chuid the server |
|
73 |
|
74 # Include defaults if available |
|
75 if [ -f /etc/default/$NAME ] ; then |
|
76 . /etc/default/$NAME |
|
77 fi |
|
78 |
|
79 # Use this if you want the user to explicitly set 'RUN' in |
|
80 # /etc/default/ |
|
81 #if [ "x$RUN" != "xyes" ] ; then |
|
82 # log_failure_msg "$NAME disabled, please adjust the configuration to your needs " |
|
83 # log_failure_msg "and then set RUN to 'yes' in /etc/default/$NAME to enable it." |
|
84 # exit 1 |
|
85 #fi |
|
86 |
|
87 # Check that the user exists (if we set a user) |
|
88 # Does the user exist? |
|
89 if [ -n "$DAEMONUSER" ] ; then |
|
90 if getent passwd | grep -q "^$DAEMONUSER:"; then |
|
91 # Obtain the uid and gid |
|
92 DAEMONUID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $3}'` |
|
93 DAEMONGID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $4}'` |
|
94 else |
|
95 log_failure_msg "The user $DAEMONUSER, required to run $NAME does not exist." |
|
96 exit 1 |
|
97 fi |
|
98 fi |
|
99 |
|
100 |
|
101 set -e |
|
102 |
|
103 running_pid() { |
|
104 # Check if a given process pid's cmdline matches a given name |
|
105 pid=$1 |
|
106 name=$2 |
|
107 [ -z "$pid" ] && return 1 |
|
108 [ ! -d /proc/$pid ] && return 1 |
|
109 cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1` |
|
110 # Is this the expected server |
|
111 [ "$cmd" != "$name" ] && return 1 |
|
112 return 0 |
|
113 } |
|
114 |
|
115 running() { |
|
116 # Check if the process is running looking at /proc |
|
117 # (works for all users) |
|
118 |
|
119 # No pidfile, probably no daemon present |
|
120 [ ! -f "$PIDFILE" ] && return 1 |
|
121 pid=`cat $PIDFILE` |
|
122 running_pid $pid $DAEMON || return 1 |
|
123 return 0 |
|
124 } |
|
125 |
|
126 start_server() { |
|
127 # Start the process using the wrapper |
|
128 if [ -z "$DAEMONUSER" ] ; then |
|
129 start_daemon -p $PIDFILE $DAEMON -- $DAEMON_OPTS |
|
130 errcode=$? |
|
131 else |
|
132 # if we are using a daemonuser then change the user id |
|
133 start-stop-daemon --start --quiet --pidfile $PIDFILE \ |
|
134 --chuid $DAEMONUSER \ |
|
135 --exec $DAEMON -- $DAEMON_OPTS |
|
136 errcode=$? |
|
137 fi |
|
138 return $errcode |
|
139 } |
|
140 |
|
141 stop_server() { |
|
142 # Stop the process using the wrapper |
|
143 if [ -z "$DAEMONUSER" ] ; then |
|
144 killproc -p $PIDFILE $DAEMON |
|
145 errcode=$? |
|
146 else |
|
147 # if we are using a daemonuser then look for process that match |
|
148 start-stop-daemon --stop --quiet --pidfile $PIDFILE \ |
|
149 --user $DAEMONUSER \ |
|
150 --exec $DAEMON |
|
151 errcode=$? |
|
152 fi |
|
153 |
|
154 return $errcode |
|
155 } |
|
156 |
|
157 reload_server() { |
|
158 [ ! -f "$PIDFILE" ] && return 1 |
|
159 pid=pidofproc $PIDFILE # This is the daemon's pid |
|
160 # Send a SIGHUP |
|
161 kill -1 $pid |
|
162 return $? |
|
163 } |
|
164 |
|
165 force_stop() { |
|
166 # Force the process to die killing it manually |
|
167 [ ! -e "$PIDFILE" ] && return |
|
168 if running ; then |
|
169 kill -15 $pid |
|
170 # Is it really dead? |
|
171 sleep "$DIETIME"s |
|
172 if running ; then |
|
173 kill -9 $pid |
|
174 sleep "$DIETIME"s |
|
175 if running ; then |
|
176 echo "Cannot kill $NAME (pid=$pid)!" |
|
177 exit 1 |
|
178 fi |
|
179 fi |
|
180 fi |
|
181 rm -f $PIDFILE |
|
182 } |
|
183 |
|
184 |
|
185 case "$1" in |
|
186 start) |
|
187 log_daemon_msg "Starting $DESC " "$NAME" |
|
188 # Check if it's running first |
|
189 if running ; then |
|
190 log_progress_msg "apparently already running" |
|
191 log_end_msg 0 |
|
192 exit 0 |
|
193 fi |
|
194 if start_server ; then |
|
195 # NOTE: Some servers might die some time after they start, |
|
196 # this code will detect this issue if STARTTIME is set |
|
197 # to a reasonable value |
|
198 [ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time |
|
199 if running ; then |
|
200 # It's ok, the server started and is running |
|
201 log_end_msg 0 |
|
202 else |
|
203 # It is not running after we did start |
|
204 log_end_msg 1 |
|
205 fi |
|
206 else |
|
207 # Either we could not start it |
|
208 log_end_msg 1 |
|
209 fi |
|
210 ;; |
|
211 stop) |
|
212 log_daemon_msg "Stopping $DESC" "$NAME" |
|
213 if running ; then |
|
214 # Only stop the server if we see it running |
|
215 errcode=0 |
|
216 stop_server || errcode=$? |
|
217 log_end_msg $errcode |
|
218 else |
|
219 # If it's not running don't do anything |
|
220 log_progress_msg "apparently not running" |
|
221 log_end_msg 0 |
|
222 exit 0 |
|
223 fi |
|
224 ;; |
|
225 force-stop) |
|
226 # First try to stop gracefully the program |
|
227 $0 stop |
|
228 if running; then |
|
229 # If it's still running try to kill it more forcefully |
|
230 log_daemon_msg "Stopping (force) $DESC" "$NAME" |
|
231 errcode=0 |
|
232 force_stop || errcode=$? |
|
233 log_end_msg $errcode |
|
234 fi |
|
235 ;; |
|
236 restart|force-reload) |
|
237 log_daemon_msg "Restarting $DESC" "$NAME" |
|
238 errcode=0 |
|
239 stop_server || errcode=$? |
|
240 # Wait some sensible amount, some server need this |
|
241 [ -n "$DIETIME" ] && sleep $DIETIME |
|
242 start_server || errcode=$? |
|
243 [ -n "$STARTTIME" ] && sleep $STARTTIME |
|
244 running || errcode=$? |
|
245 log_end_msg $errcode |
|
246 ;; |
|
247 status) |
|
248 |
|
249 log_daemon_msg "Checking status of $DESC" "$NAME" |
|
250 if running ; then |
|
251 log_progress_msg "running" |
|
252 log_end_msg 0 |
|
253 else |
|
254 log_progress_msg "apparently not running" |
|
255 log_end_msg 1 |
|
256 exit 1 |
|
257 fi |
|
258 ;; |
|
259 # Use this if the daemon cannot reload |
|
260 reload) |
|
261 log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon" |
|
262 log_warning_msg "cannot re-read the config file (use restart)." |
|
263 ;; |
|
264 # And this if it cann |
|
265 #reload) |
|
266 # |
|
267 # If the daemon can reload its config files on the fly |
|
268 # for example by sending it SIGHUP, do it here. |
|
269 # |
|
270 # If the daemon responds to changes in its config file |
|
271 # directly anyway, make this a do-nothing entry. |
|
272 # |
|
273 # log_daemon_msg "Reloading $DESC configuration files" "$NAME" |
|
274 # if running ; then |
|
275 # reload_server |
|
276 # if ! running ; then |
|
277 # Process died after we tried to reload |
|
278 # log_progress_msg "died on reload" |
|
279 # log_end_msg 1 |
|
280 # exit 1 |
|
281 # fi |
|
282 # else |
|
283 # log_progress_msg "server is not running" |
|
284 # log_end_msg 1 |
|
285 # exit 1 |
|
286 # fi |
|
287 #;; |
|
288 |
|
289 *) |
|
290 N=/etc/init.d/$NAME |
|
291 echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2 |
|
292 exit 1 |
|
293 ;; |
|
294 esac |
|
295 |
|
296 exit 0 |