optimize CDR rating after timed rate perf regression, RT#15739
[freeside.git] / debian / init.d.lsb.ex
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:          freeside
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/freeside # 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/freeside  # Log directory to use
45
46 PIDFILE=/var/run/$NAME.pid 
47
48 test -x $DAEMON || exit 0
49 test -x $DAEMON_WRAPPER || exit 0
50
51 . /lib/lsb/init-functions
52
53 # Default options, these can be overriden by the information
54 # at /etc/default/$NAME
55 DAEMON_OPTS=""          # Additional options given to the server 
56
57 DODTIME=10              # Time to wait for the server to die, in seconds
58                         # If this value is set too low you might not
59                         # let some servers to die gracefully and
60                         # 'restart' will not work
61                         
62 LOGFILE=$LOGDIR/$NAME.log  # Server logfile
63 #DAEMONUSER=freeside   # Users to run the daemons as. If this value
64                         # is set start-stop-daemon will chuid the server
65
66 # Include defaults if available
67 if [ -f /etc/default/$NAME ] ; then
68         . /etc/default/$NAME
69 fi
70
71 # Use this if you want the user to explicitly set 'RUN' in 
72 # /etc/default/
73 #if [ "x$RUN" != "xyes" ] ; then
74 #    log_failure_msg "$NAME disabled, please adjust the configuration to your needs "
75 #    log_failure_msg "and then set RUN to 'yes' in /etc/default/$NAME to enable it."
76 #    exit 1
77 #fi
78
79 # Check that the user exists (if we set a user)
80 # Does the user exist?
81 if [ -n "$DAEMONUSER" ] ; then
82     if getent passwd | grep -q "^$DAEMONUSER:"; then
83         # Obtain the uid and gid
84         DAEMONUID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $3}'`
85         DAEMONGID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $4}'`
86     else
87         log_failure_msg "The user $DAEMONUSER, required to run $NAME does not exist."
88         exit 1
89     fi
90 fi
91
92
93 set -e
94
95 running_pid() {
96 # Check if a given process pid's cmdline matches a given name
97     pid=$1
98     name=$2
99     [ -z "$pid" ] && return 1 
100     [ ! -d /proc/$pid ] &&  return 1
101     cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
102     # Is this the expected server
103     [ "$cmd" != "$name" ] &&  return 1
104     return 0
105 }
106
107 running() {
108 # Check if the process is running looking at /proc
109 # (works for all users)
110
111     # No pidfile, probably no daemon present
112     [ ! -f "$PIDFILE" ] && return 1
113     pid=`cat $PIDFILE`
114     running_pid $pid $DAEMON_WRAPPER || return 1
115     return 0
116 }
117
118 start_server() {
119 # Start the process using the wrapper
120         if [ -z "$DAEMONUSER" ] ; then
121             start-stop-daemon --start --quiet --pidfile $PIDFILE \
122                         --exec $DAEMON -- $DAEMON_OPTS
123             errcode=$?
124         else
125 # if we are using a daemonuser then change the user id
126             start-stop-daemon --start --quiet --pidfile $PIDFILE \
127                         --chuid $DAEMONUSER \
128                         --exec $DAEMON -- $DAEMON_OPTS
129             errcode=$?
130         fi
131         return $errcode
132 }
133
134 stop_server() {
135 # Stop the process using the wrapper
136         if [ -z "$DAEMONUSER" ] ; then
137             start-stop-daemon --stop --quiet --pidfile $PIDFILE \
138                         --exec $DAEMON
139             errcode=$
140         else
141 # if we are using a daemonuser then look for process that match
142             start-stop-daemon --stop --quiet --pidfile $PIDFILE \
143                         --user $DAEMONUSER \
144                         --exec $DAEMON
145             errcode=$
146         fi
147
148         return $errcode
149 }
150
151 reload_server() {
152     [ ! -f "$PIDFILE" ] && return 1
153     pid=`cat $PIDFILE` # This is the daemon's pid
154     # Send a SIGHUP
155     kill -1 $pid
156     return $?
157 }
158
159 force_stop() {
160 # Force the process to die killing it manually
161         [ ! -e "$PIDFILE" ] && return
162         if running ; then
163                 kill -15 $pid
164         # Is it really dead?
165                 sleep "$DIETIME"s
166                 if running ; then
167                         kill -9 $pid
168                         sleep "$DIETIME"s
169                         if running ; then
170                                 echo "Cannot kill $NAME (pid=$pid)!"
171                                 exit 1
172                         fi
173                 fi
174         fi
175         rm -f $PIDFILE
176 }
177
178
179 case "$1" in
180   start)
181         log_daemon_msg "Starting $DESC " "$NAME"
182         # Check if it's running first
183         if running ;  then
184             log_progress_msg "apparently already running"
185             log_end_msg 0
186             exit 0
187         fi
188         if start_server && running ;  then
189             # It's ok, the server started and is running
190             log_end_msg 0
191         else
192             # Either we could not start it or it is not running
193             # after we did
194             # NOTE: Some servers might die some time after they start,
195             # this code does not try to detect this and might give
196             # a false positive (use 'status' for that)
197             log_end_msg 1
198         fi
199         ;;
200   stop)
201         log_daemon_msg "Stopping $DESC" "$NAME"
202         if running ; then
203             # Only stop the server if we see it running
204             stop_server
205             log_end_msg $?
206         else
207             # If it's not running don't do anything
208             log_progress_msg "apparently not running"
209             log_end_msg 0
210             exit 0
211         fi
212         ;;
213   force-stop)
214         # First try to stop gracefully the program
215         $0 stop
216         if running; then
217             # If it's still running try to kill it more forcefully
218             log_daemon_msg "Stopping (force) $DESC" "$NAME"
219             force_stop
220             log_end_msg $?
221         fi
222         ;;
223   restart|force-reload)
224         log_daemon_msg "Restarting $DESC" "$NAME"
225         stop_server
226         # Wait some sensible amount, some server need this
227         [ -n "$DIETIME" ] && sleep $DIETIME
228         start_server
229         running
230         log_end_msg $?
231         ;;
232   status)
233
234         log_daemon_msg "Checking status of $DESC" "$NAME"
235         if running ;  then
236             log_progress_msg "running"
237             log_end_msg 0
238         else
239             log_progress_msg "apparently not running"
240             log_end_msg 1
241             exit 1
242         fi
243         ;;
244   # Use this if the daemon cannot reload
245   reload)
246         log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
247         log_warning_msg "cannot re-read the config file (use restart)."
248         ;;
249   # And this if it cann
250   #reload)
251           #
252           # If the daemon can reload its config files on the fly
253           # for example by sending it SIGHUP, do it here.
254           #
255           # If the daemon responds to changes in its config file
256           # directly anyway, make this a do-nothing entry.
257           #
258           # log_daemon_msg "Reloading $DESC configuration files" "$NAME"
259           # if running ; then
260           #    reload_server
261           #    if ! running ;  then
262           # Process died after we tried to reload
263           #       log_progress_msg "died on reload"
264           #       log_end_msg 1
265           #       exit 1
266           #    fi
267           # else
268           #    log_progress_msg "server is not running"
269           #    log_end_msg 1
270           #    exit 1
271           # fi
272                                                                                     #;;
273
274   *)
275         N=/etc/init.d/$NAME
276         echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
277         exit 1
278         ;;
279 esac
280
281 exit 0