File indexing completed on 2024-05-12 05:39:33

0001 #!/bin/sh
0002 #
0003 # Example init.d script with LSB support.
0004 #
0005 # Please read this init.d carefully and modify the sections to
0006 # adjust it to the program you want to run.
0007 #
0008 # Copyright (c) 2007 Javier Fernandez-Sanguino <jfs@debian.org>
0009 #
0010 # This is free software; you may redistribute it and/or modify
0011 # it under the terms of the GNU General Public License as
0012 # published by the Free Software Foundation; either version 2,
0013 # or (at your option) any later version.
0014 #
0015 # This is distributed in the hope that it will be useful, but
0016 # WITHOUT ANY WARRANTY; without even the implied warranty of
0017 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018 # GNU General Public License for more details.
0019 #
0020 # You should have received a copy of the GNU General Public License with
0021 # the Debian operating system, in /usr/share/common-licenses/GPL;  if
0022 # not, write to the Free Software Foundation, Inc., 59 Temple Place,
0023 # Suite 330, Boston, MA 02111-1307 USA
0024 #
0025 ### BEGIN INIT INFO
0026 # Provides:          rolisteam
0027 # Required-Start:    $network $local_fs
0028 # Required-Stop:
0029 # Should-Start:      $named
0030 # Should-Stop:
0031 # Default-Start:     2 3 4 5
0032 # Default-Stop:      0 1 6
0033 # Short-Description: <Enter a short description of the sortware>
0034 # Description:       <Enter a long description of the software>
0035 #                    <...>
0036 #                    <...>
0037 ### END INIT INFO
0038 
0039 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
0040 
0041 DAEMON=/usr/sbin/rolisteam # Introduce the server's location here
0042 NAME=#PACKAGE              # Introduce the short server's name here
0043 DESC=#PACKAGE              # Introduce a short description here
0044 LOGDIR=/var/log/rolisteam  # Log directory to use
0045 
0046 PIDFILE=/var/run/$NAME.pid
0047 
0048 test -x $DAEMON || exit 0
0049 
0050 . /lib/lsb/init-functions
0051 
0052 # Default options, these can be overriden by the information
0053 # at /etc/default/$NAME
0054 DAEMON_OPTS=""          # Additional options given to the server
0055 
0056 DIETIME=10              # Time to wait for the server to die, in seconds
0057                         # If this value is set too low you might not
0058                         # let some servers to die gracefully and
0059                         # 'restart' will not work
0060 
0061 #STARTTIME=2             # Time to wait for the server to start, in seconds
0062                         # If this value is set each time the server is
0063                         # started (on start or restart) the script will
0064                         # stall to try to determine if it is running
0065                         # If it is not set and the server takes time
0066                         # to setup a pid file the log message might
0067                         # be a false positive (says it did not start
0068                         # when it actually did)
0069 
0070 LOGFILE=$LOGDIR/$NAME.log  # Server logfile
0071 #DAEMONUSER=rolisteam   # Users to run the daemons as. If this value
0072                         # is set start-stop-daemon will chuid the server
0073 
0074 # Include defaults if available
0075 if [ -f /etc/default/$NAME ] ; then
0076     . /etc/default/$NAME
0077 fi
0078 
0079 # Use this if you want the user to explicitly set 'RUN' in
0080 # /etc/default/
0081 #if [ "x$RUN" != "xyes" ] ; then
0082 #    log_failure_msg "$NAME disabled, please adjust the configuration to your needs "
0083 #    log_failure_msg "and then set RUN to 'yes' in /etc/default/$NAME to enable it."
0084 #    exit 1
0085 #fi
0086 
0087 # Check that the user exists (if we set a user)
0088 # Does the user exist?
0089 if [ -n "$DAEMONUSER" ] ; then
0090     if getent passwd | grep -q "^$DAEMONUSER:"; then
0091         # Obtain the uid and gid
0092         DAEMONUID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $3}'`
0093         DAEMONGID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $4}'`
0094     else
0095         log_failure_msg "The user $DAEMONUSER, required to run $NAME does not exist."
0096         exit 1
0097     fi
0098 fi
0099 
0100 
0101 set -e
0102 
0103 running_pid() {
0104 # Check if a given process pid's cmdline matches a given name
0105     pid=$1
0106     name=$2
0107     [ -z "$pid" ] && return 1
0108     [ ! -d /proc/$pid ] &&  return 1
0109     cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
0110     # Is this the expected server
0111     [ "$cmd" != "$name" ] &&  return 1
0112     return 0
0113 }
0114 
0115 running() {
0116 # Check if the process is running looking at /proc
0117 # (works for all users)
0118 
0119     # No pidfile, probably no daemon present
0120     [ ! -f "$PIDFILE" ] && return 1
0121     pid=`cat $PIDFILE`
0122     running_pid $pid $DAEMON || return 1
0123     return 0
0124 }
0125 
0126 start_server() {
0127 # Start the process using the wrapper
0128         if [ -z "$DAEMONUSER" ] ; then
0129             start_daemon -p $PIDFILE $DAEMON $DAEMON_OPTS
0130             errcode=$?
0131         else
0132 # if we are using a daemonuser then change the user id
0133             start-stop-daemon --start --quiet --pidfile $PIDFILE \
0134                         --chuid $DAEMONUSER \
0135                         --exec $DAEMON -- $DAEMON_OPTS
0136             errcode=$?
0137         fi
0138         return $errcode
0139 }
0140 
0141 stop_server() {
0142 # Stop the process using the wrapper
0143         if [ -z "$DAEMONUSER" ] ; then
0144             killproc -p $PIDFILE $DAEMON
0145             errcode=$?
0146         else
0147 # if we are using a daemonuser then look for process that match
0148             start-stop-daemon --stop --quiet --pidfile $PIDFILE \
0149                         --user $DAEMONUSER \
0150                         --exec $DAEMON
0151             errcode=$?
0152         fi
0153 
0154         return $errcode
0155 }
0156 
0157 reload_server() {
0158     [ ! -f "$PIDFILE" ] && return 1
0159     pid=pidofproc $PIDFILE # This is the daemon's pid
0160     # Send a SIGHUP
0161     kill -1 $pid
0162     return $?
0163 }
0164 
0165 force_stop() {
0166 # Force the process to die killing it manually
0167     [ ! -e "$PIDFILE" ] && return
0168     if running ; then
0169         kill -15 $pid
0170         # Is it really dead?
0171         sleep "$DIETIME"s
0172         if running ; then
0173             kill -9 $pid
0174             sleep "$DIETIME"s
0175             if running ; then
0176                 echo "Cannot kill $NAME (pid=$pid)!"
0177                 exit 1
0178             fi
0179         fi
0180     fi
0181     rm -f $PIDFILE
0182 }
0183 
0184 
0185 case "$1" in
0186   start)
0187         log_daemon_msg "Starting $DESC " "$NAME"
0188         # Check if it's running first
0189         if running ;  then
0190             log_progress_msg "apparently already running"
0191             log_end_msg 0
0192             exit 0
0193         fi
0194         if start_server ; then
0195             # NOTE: Some servers might die some time after they start,
0196             # this code will detect this issue if STARTTIME is set
0197             # to a reasonable value
0198             [ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time 
0199             if  running ;  then
0200                 # It's ok, the server started and is running
0201                 log_end_msg 0
0202             else
0203                 # It is not running after we did start
0204                 log_end_msg 1
0205             fi
0206         else
0207             # Either we could not start it
0208             log_end_msg 1
0209         fi
0210         ;;
0211   stop)
0212         log_daemon_msg "Stopping $DESC" "$NAME"
0213         if running ; then
0214             # Only stop the server if we see it running
0215             errcode=0
0216             stop_server || errcode=$?
0217             log_end_msg $errcode
0218         else
0219             # If it's not running don't do anything
0220             log_progress_msg "apparently not running"
0221             log_end_msg 0
0222             exit 0
0223         fi
0224         ;;
0225   force-stop)
0226         # First try to stop gracefully the program
0227         $0 stop
0228         if running; then
0229             # If it's still running try to kill it more forcefully
0230             log_daemon_msg "Stopping (force) $DESC" "$NAME"
0231             errcode=0
0232             force_stop || errcode=$?
0233             log_end_msg $errcode
0234         fi
0235         ;;
0236   restart|force-reload)
0237         log_daemon_msg "Restarting $DESC" "$NAME"
0238         errcode=0
0239         stop_server || errcode=$?
0240         # Wait some sensible amount, some server need this
0241         [ -n "$DIETIME" ] && sleep $DIETIME
0242         start_server || errcode=$?
0243         [ -n "$STARTTIME" ] && sleep $STARTTIME
0244         running || errcode=$?
0245         log_end_msg $errcode
0246         ;;
0247   status)
0248 
0249         log_daemon_msg "Checking status of $DESC" "$NAME"
0250         if running ;  then
0251             log_progress_msg "running"
0252             log_end_msg 0
0253         else
0254             log_progress_msg "apparently not running"
0255             log_end_msg 1
0256             exit 1
0257         fi
0258         ;;
0259   # Use this if the daemon cannot reload
0260   reload)
0261         log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
0262         log_warning_msg "cannot re-read the config file (use restart)."
0263         ;;
0264   # And this if it cann
0265   #reload)
0266           #
0267           # If the daemon can reload its config files on the fly
0268           # for example by sending it SIGHUP, do it here.
0269           #
0270           # If the daemon responds to changes in its config file
0271           # directly anyway, make this a do-nothing entry.
0272           #
0273           # log_daemon_msg "Reloading $DESC configuration files" "$NAME"
0274           # if running ; then
0275           #    reload_server
0276           #    if ! running ;  then
0277           # Process died after we tried to reload
0278           #       log_progress_msg "died on reload"
0279           #       log_end_msg 1
0280           #       exit 1
0281           #    fi
0282           # else
0283           #    log_progress_msg "server is not running"
0284           #    log_end_msg 1
0285           #    exit 1
0286           # fi
0287                                                                                     #;;
0288 
0289   *)
0290         N=/etc/init.d/$NAME
0291         echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
0292         exit 1
0293         ;;
0294 esac
0295 
0296 exit 0