xref: /trafficserver/rc/trafficserver.in (revision a19cda74)
1#!/bin/sh
2#
3# Licensed to the Apache Software Foundation (ASF) under one or more
4# contributor license agreements.  See the NOTICE file distributed with
5# this work for additional information regarding copyright ownership.
6# The ASF licenses this file to You under the Apache License, Version 2.0
7# (the "License"); you may not use this file except in compliance with
8# the License.  You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18# chkconfig: - 90 10
19# description: Apache Traffic Server is a fast, scalable and extensible  \
20#             HTTP/1.1 compliant caching proxy server.
21# processname: traffic_cop traffic_manager traffic_server
22# config: /etc/trafficserver
23# config: /etc/sysconfig/trafficserver
24# pidfile: /var/run/trafficserver/cop.pid
25#
26# Template from /etc/init.d/skeleton on Ubuntu
27#
28### BEGIN INIT INFO
29# Provides:          Traffic Server
30# Required-Start:    $local_fs $remote_fs
31# Required-Stop:     $local_fs $remote_fs
32# Default-Start:     2 3 4 5
33# Default-Stop:      S 0 1 6
34# Short-Description: Startup/shutdown script for the Apache Traffic Server
35# Description:       Apache Traffic Server is fast, scalable and extensible
36#                    HTTP/1.1 compliant caching proxy server.
37### END INIT INFO
38
39# Fedora:
40#  When this file is installed as /etc/rc.d/init.d/trafficserver
41#  and added to the chkconfig system with `chkconfig --add trafficserver`
42#  then the default values of these variables can be overridden
43#  in the /etc/sysconfig/trafficserver file
44# Ubuntu:
45#  Values read from /etc/default/trafficserver
46
47TS_PACKAGE_NAME="@PACKAGE_NAME@"
48TS_PACKAGE_VERSION="@PACKAGE_VERSION@"
49TS_PACKAGE_BUGREPORT="@PACKAGE_BUGREPORT@"
50UNAME_S=$(uname -s)
51
52if [ -e /etc/SuSE-release ]; then
53    DISTRIB_ID="suse"
54elif [ -e /etc/fedora-release ]; then
55    DISTRIB_ID="fedora"
56elif [ -e /etc/gentoo-release ]; then
57    DISTRIB_ID="gentoo"
58elif [ -e /etc/redhat-release ]; then
59    DISTRIB_ID="redhat" # also exists on Fedora
60elif [ -e /etc/lsb-release ]; then
61    DISTRIB_ID="ubuntu"
62elif [ -e /etc/debian_version ]; then
63    DISTRIB_ID="debian" # also exists on Ubuntu
64elif [ -e /etc/slackware-version ]; then
65    DISTRIB_ID="slackware"
66elif [ "$UNAME_S" = "Darwin" ]; then
67    DISTRIB_ID="Darwin"
68elif [ "$UNAME_S" = "FreeBSD" ]; then
69    DISTRIB_ID="FreeBSD"
70fi
71
72# We might want to move over to use /etc/system-release for more distros?
73# I know at least Fedora supports it as well.
74if [ "" = "$DISTRIB_ID" -a -e /etc/system-release ]; then
75    case $(cat /etc/system-release) in
76	*Amazon*)
77	    DISTRIB_ID="redhat" # Amazon AMI is mostly like RHEL
78	    ;;
79	*)
80	    ;;
81    esac
82fi
83
84# /usr/bin/lsb_release -rd # Ubuntu release & description info
85
86HEAD=/usr/bin/head
87test -x $HEAD || HEAD=head
88ESED=/usr/bin/sed
89test -x $ESED || ESED=sed
90
91# Ubuntu: Do NOT "set -e"
92
93#Ubuntu:PATH should only include /usr/* if it runs after the mountnfs.sh script
94PATH=/usr/sbin:/usr/bin:/sbin:/bin
95DESC="start/stop Traffic Server"
96NAME=trafficserver
97SLEEP_TIME=5
98TS_PREFIX="@prefix@"
99
100TS_ROOT=${TS_ROOT:-$TS_PREFIX}
101# TS_BASE is offset inside the file system from where the layout starts
102# For standard installations TS_BASE will be empty
103eval TS_BASE="`echo $TS_ROOT | ${ESED} -e 's;@prefix@$;;'`"
104
105TC_NAME=${TC_NAME:-traffic_cop}
106TM_NAME=${TM_NAME:-traffic_manager}
107TS_NAME=${TS_NAME:-traffic_server}
108TC_DAEMON=${TC_DAEMON:-$TS_BASE@exp_bindir@/traffic_cop}
109TC_DAEMON_ARGS=""
110TM_DAEMON=${TM_DAEMON:-$TS_BASE@exp_bindir@/traffic_manager}
111TM_DAEMON_ARGS=""
112TS_DAEMON=${TS_DAEMON:-$TS_BASE@exp_bindir@/traffic_server}
113TS_DAEMON_ARGS=""
114TL_BINARY=${TL_BINARY:-$TS_BASE@exp_bindir@/traffic_ctl}
115TC_PIDFILE=${TC_PIDFILE:-$TS_BASE@exp_runtimedir@/cop.lock}
116TM_PIDFILE=${TM_PIDFILE:-$TS_BASE@exp_runtimedir@/manager.lock}
117TS_PIDFILE=${TS_PIDFILE:-$TS_BASE@exp_runtimedir@/server.lock}
118# number of times to retry check on pid lock file
119PIDFILE_CHECK_RETRIES=${PIDFILE_CHECK_RETRIES:-30}
120# stdout file of executable
121STDOUTLOG=${STDOUTLOG:-$TS_BASE@exp_logdir@/traffic_server.stdout}
122# stderr file of executable
123STDERRLOG=${STDERRLOG:-$TS_BASE@exp_logdir@/traffic_server.stderr}
124
125if [ -d /etc/rc.d/init.d ]; then
126    SCRIPTNAME=/etc/rc.d/init.d/$NAME # Fedora
127elif [ -d /etc/init.d ]; then
128    SCRIPTNAME=/etc/init.d/$NAME # Ubuntu
129elif [ "$DISTRIB_ID" = "Darwin" ]; then
130    SCRIPTNAME=$0 # Darwin
131elif [ "$DISTRIB_ID" = "FreeBSD" ]; then
132    SCRIPTNAME=$0 # FreeBSD
133else
134    echo "This script needs to be ported to this OS"
135    exit 1
136fi
137
138# Exit if the package can not be found
139if [ ! -x "$TS_DAEMON" ]; then
140    echo "Error: Can not find $TS_DAEMON using TS_ROOT=$TS_ROOT"
141    echo "       Please set TS_ROOT to point to base installation."
142    exit 0
143fi
144
145# Ubuntu: Read configuration variable file if it is present
146test -r /etc/default/$NAME && . /etc/default/$NAME
147
148# RHEL/Fedora: Read configuration variable file if it is present
149test -r /etc/sysconfig/$NAME && . /etc/sysconfig/$NAME
150
151# Gentoo: Read configuration variable file if it is present
152test -r /etc/conf.d/$NAME && . /etc/conf.d/$NAME
153
154# Ubuntu: Load the VERBOSE setting and other rcS variables
155test -f /etc/default/rcS && . /etc/default/rcS
156
157# Suse: load rc.status
158test -f /etc/rc.status && . /etc/rc.status
159
160# Fedora: make avail helper functions 'action', 'killproc', 'status', etc
161test -f /etc/rc.d/init.d/functions && . /etc/rc.d/init.d/functions
162
163# Gentoo: make avail helper function 'ebegin', 'eend', etc
164test -f /etc/init.d/functions.sh && . /etc/init.d/functions.sh
165
166# Ubuntu: Define LSB log_* functions.
167#         Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
168test -f /lib/lsb/init-functions && . /lib/lsb/init-functions
169
170# FreeBSD
171# NB: FreeBSD's ports system installs a run script in /usr/local/etc/rc.d/
172# with native OS rc.subr(8) features.
173test -f /etc/rc.subr && . /etc/rc.subr
174
175if [ ! -d $TS_BASE@exp_runtimedir@ ]
176then
177	mkdir -p $TS_BASE@exp_runtimedir@
178	chown @pkgsysuser@:@pkgsysgroup@ $TS_BASE@exp_runtimedir@
179fi
180
181#
182# Fedora:
183#  launches $* in the background
184#  and checks if program creates its own pid lock file
185#  redirects stdout and stderr to files
186#  this function is meant to substitute 'daemon'
187#  because 'daemon' expects $1 to fork itself
188forkdaemon()
189{
190    local i=0
191
192    # launch in background, i.e. fork
193    # and redirect stdout and stderr to files
194    $* >> $STDOUTLOG 2>> $STDERRLOG &
195
196    while (( $i < $PIDFILE_CHECK_RETRIES ))
197    do
198        # check for regular file and size greater than 0
199        if [[ -f $TC_PIDFILE ]] && [[ -s $TC_PIDFILE ]]
200        then
201            success
202            return 0
203        fi
204
205        sleep 1  # zzz for 1 second
206        (( i++ ))
207
208    done
209
210    failure
211    return 1
212}
213
214
215#
216# Ubuntu: Function that starts the daemon/service
217#
218do_start()
219{
220  # Return
221  #   0 if daemon has been started
222  #   1 if daemon was already running
223  #   2 if daemon could not be started
224    start-stop-daemon --start --quiet --pidfile $TC_PIDFILE --exec $TC_DAEMON --test > /dev/null \
225        || return 1
226    start-stop-daemon --start --background --quiet --pidfile $TC_PIDFILE --exec $TC_DAEMON -- \
227        $TC_DAEMON_ARGS \
228        || return 2
229  # Add code here, if necessary, that waits for the process to be ready
230  # to handle requests from services started subsequently which depend
231  # on this one.  As a last resort, sleep for some time.
232    sleep 1
233    test -f "$TC_PIDFILE" || return 2
234}
235
236#
237# Ubuntu: Function that stops the daemon/service
238#
239do_stop()
240{
241  # Return
242  #   0 if daemon has been stopped
243  #   1 if daemon was already stopped
244  #   2 if daemon could not be stopped
245  #   other if a failure occurred
246    start-stop-daemon --stop --quiet --retry=QUIT/30/KILL/5 --pidfile $TC_PIDFILE --name $TC_NAME
247    RETVAL="$?"
248    test "$RETVAL" != 0 && return $RETVAL
249  # Wait for children to finish too if this is a daemon that forks
250  # and if the daemon is only ever run from this initscript.
251  # If the above conditions are not satisfied then add some other code
252  # that waits for the process to drop all resources that could be
253  # needed by services started subsequently.  A last resort is to
254  # sleep for some time.
255    start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $TC_DAEMON
256    RETVAL="$?"
257    test "$RETVAL" != 0 && return $RETVAL
258  # Need to stop the TM and TS also
259    start-stop-daemon --stop --quiet --oknodo --retry=QUIT/30/KILL/5 --pidfile $TM_PIDFILE --name $TM_NAME
260    RETVAL="$?"
261    test "$RETVAL" != 0 && return $RETVAL
262    start-stop-daemon --stop --quiet --oknodo --retry=QUIT/30/KILL/5 --pidfile $TS_PIDFILE --name $TS_NAME
263    RETVAL="$?"
264    test "$RETVAL" != 0 && return $RETVAL
265  # Many daemons don't delete their pidfiles when they exit.
266    rm -f $TC_PIDFILE
267    rm -f $TM_PIDFILE
268    rm -f $TS_PIDFILE
269    return "$RETVAL"
270}
271
272# main
273case "$1" in
274    start)
275        if [ "$DISTRIB_ID" = "ubuntu" -o "$DISTRIB_ID" = "debian" ] ; then
276            test "x$VERBOSE" != "xno" && log_daemon_msg "Starting ${TS_PACKAGE_NAME}" "$NAME"
277            retval=0
278            do_start
279            test "$?" -ne 0 -a "$?" -ne 1 && retval=1
280            test "x$VERBOSE" != "xno" && log_end_msg "$retval"
281            exit "$retval"
282        elif [ "$DISTRIB_ID" = "gentoo" ]; then
283	    ebegin "Starting ${TS_PACKAGE_NAME}"
284	    do_start
285	    eend $?
286        elif [ "$DISTRIB_ID" = "fedora" -o "$DISTRIB_ID" = "redhat" ]; then
287            action "Starting ${TS_PACKAGE_NAME}:" forkdaemon $TC_DAEMON $TC_DAEMON_ARGS
288        elif [ "$DISTRIB_ID" = "suse" ]; then
289            echo -n "Starting ${TS_PACKAGE_NAME}"
290            startproc -p $TC_PIDFILE $TC_DAEMON $TC_DAEMON_ARGS
291            rc_status -v
292        elif [ "$DISTRIB_ID" = "Darwin" ]; then
293            echo "Starting ${TS_PACKAGE_NAME}"
294            launchctl bsexec / launchctl list $TC_NAME > /dev/null 2>&1 && exit 0
295            launchctl bsexec / launchctl submit -l $TC_NAME -p $TC_DAEMON -o $STDOUTLOG -e $STDERRLOG -- $TC_DAEMON_ARGS
296        elif [ "$DISTRIB_ID" = "FreeBSD" ]; then
297            echo "Starting ${TS_PACKAGE_NAME}"
298            name="$TC_NAME"
299            command="/usr/sbin/daemon"
300            command_args="$TC_DAEMON $TC_DAEMON_ARGS"
301            pidfile="$TC_PIDFILE"
302            run_rc_command "$1"
303        else
304            echo "This script needs to be ported to this OS"
305            exit 1
306        fi
307        ;;
308    stop)
309        if [ "$DISTRIB_ID" = "ubuntu" -o "$DISTRIB_ID" = "debian" ] ; then
310            test "x$VERBOSE" != "xno" && log_daemon_msg "Stopping ${TS_PACKAGE_NAME}" "$NAME"
311            retval=0
312            do_stop
313            test "$?" -ne 0 -a "$?" -ne 1 && retval=1
314            test "x$VERBOSE" != "xno" && log_end_msg "$retval"
315            exit "$retval"
316        elif [ "$DISTRIB_ID" = "fedora" -o "$DISTRIB_ID" = "redhat" ]; then
317            action "Stopping ${TC_NAME}:" killproc -d 35 -p $TC_PIDFILE $TC_DAEMON
318            action "Stopping ${TM_NAME}:" killproc -d 35 -p $TM_PIDFILE $TM_DAEMON
319            action "Stopping ${TS_NAME}:" killproc -d 35 -p $TS_PIDFILE $TS_DAEMON
320        elif [ "$DISTRIB_ID" = "gentoo" ]; then
321	    ebegin "Starting ${TS_PACKAGE_NAME}"
322	    do_stop
323	    eend $?
324        elif [ "$DISTRIB_ID" = "suse" ]; then
325            echo -n "Stopping ${TC_NAME}"
326            killproc -p $TC_PIDFILE $TC_DAEMON
327            rc_status -v
328            echo -n "Stopping ${TM_NAME}"
329            killproc -p $TM_PIDFILE $TM_DAEMON
330            rc_status -v
331            echo -n "Stopping ${TS_NAME}"
332            killproc -p $TS_PIDFILE $TS_DAEMON
333            rc_status -v
334        elif [ "$DISTRIB_ID" = "Darwin" ]; then
335            echo "Stopping ${TS_PACKAGE_NAME}"
336            launchctl bsexec / launchctl list $TC_NAME > /dev/null 2>&1 || exit 0
337            echo "Stopping ${TC_NAME}"
338            launchctl bsexec / launchctl remove ${TC_NAME}
339            rm -f ${TC_PIDFILE}
340            echo "Stopping ${TM_NAME}"
341            kill $(cat $TM_PIDFILE)
342            rm -f ${TM_PIDFILE}
343            echo "Stopping ${TS_NAME}"
344            kill $(cat $TS_PIDFILE)
345            rm -f ${TS_PIDFILE}
346        elif [ "$DISTRIB_ID" = "FreeBSD" ]; then
347            echo "Stopping ${TS_PACKAGE_NAME}"
348            if [ -e "$TC_PIDFILE" ]; then
349                kill $(cat $TC_PIDFILE)
350                rm -f ${TC_PIDFILE}
351            fi
352            if [ -e "$TM_PIDFILE" ]; then
353                kill $(cat $TM_PIDFILE)
354                rm -f ${TM_PIDFILE}
355            fi
356        else
357            echo "This script needs to be ported to this OS"
358            exit 1
359        fi
360        ;;
361    reload|force-reload)
362        if [ "$DISTRIB_ID" = "ubuntu" -o "$DISTRIB_ID" = "debian" ] ; then
363            test "x$VERBOSE" != "xno" && log_daemon_msg "Reloading ${TS_PACKAGE_NAME}" "$NAME"
364            retval=0
365            $TL_BINARY config reload
366            test "$?" -ne 0 -a "$?" -ne 1 && retval=1
367            test "x$VERBOSE" != "xno" && log_end_msg "$retval"
368            exit "$retval"
369        elif [ "$DISTRIB_ID" = "fedora" -o "$DISTRIB_ID" = "redhat" ]; then
370            action "Reloading ${NAME}:" $TL_BINARY config reload
371        elif [ "$DISTRIB_ID" = "gentoo" ]; then
372	    ebegin "Reloading ${NAME}"
373            $TL_BINARY config reload
374	    eend $?
375        elif [ "$DISTRIB_ID" = "suse" ]; then
376            echo -n "Reloading ${NAME}"
377            $TL_BINARY config reload
378            rc_status -v
379        elif [ "$DISTRIB_ID" = "Darwin" ]; then
380            echo "Reloading ${NAME}"
381            $TL_BINARY config reload
382        elif [ "$DISTRIB_ID" = "FreeBSD" ]; then
383            echo "Reloading ${NAME}"
384            $TL_BINARY config reload
385        else
386            echo "This script needs to be ported to this OS"
387            exit 1
388        fi
389        ;;
390    restart)
391        if [ "$DISTRIB_ID" = "ubuntu" -o "$DISTRIB_ID" = "debian" ] ; then
392      #
393      # If the "reload" option is implemented then remove the
394      # 'force-reload' alias
395      #
396            log_daemon_msg "Restarting ${TS_PACKAGE_NAME}" "$NAME"
397            do_stop
398            case "$?" in
399                0|1)
400                    sleep $SLEEP_TIME
401                    do_start
402                    case "$?" in
403                        0) log_end_msg 0 ;;
404                        1) log_end_msg 0 ;; # Daemons were already stopped
405                        *) log_end_msg 1 ;; # Failed to start
406                    esac
407                    ;;
408                *)
409            # Failed to stop
410                    log_end_msg 1
411                    ;;
412            esac
413        else
414      # This is the typical process for restart
415            sh $0 'stop'
416            sleep $SLEEP_TIME
417            sh $0 'start'
418        fi
419        ;;
420    status)
421        if [ "$DISTRIB_ID" = "fedora" -o "$DISTRIB_ID" = "redhat" ]; then
422            status -p $TC_PIDFILE $TC_NAME
423        elif [ "$DISTRIB_ID" = "ubuntu" -o "$DISTRIB_ID" = "debian" ] ; then
424            status_of_proc "$TC_DAEMON" "$TC_NAME" -p "$TC_PIDFILE" && exit 0 || exit $?
425        elif [  "$DISTRIB_ID" = "suse" ]; then
426            echo -n "Checking for service ${DM}: "
427            checkproc -p $TC_PIDFILE $TC_NAME
428            rc_status -v
429        elif [ "$DISTRIB_ID" = "Darwin" ]; then
430            /bin/echo -n "${TS_PACKAGE_NAME} is "
431            launchctl bsexec / launchctl list $TC_NAME > /dev/null 2>&1
432            status=$?
433            [ $status -eq 0 ] || /bin/echo -n "not "
434            echo "running."
435        elif [ "$DISTRIB_ID" = "FreeBSD" -o "$DISTRIB_ID" = "gentoo" ]; then
436            if pgrep $TM_NAME > /dev/null ; then
437                echo "$TM_NAME running as pid `cat $TM_PIDFILE`" ; else
438                echo "$TM_NAME not running"
439            fi
440            if pgrep $TS_NAME > /dev/null ; then
441                echo "$TS_NAME running as pid `cat $TS_PIDFILE`"; else
442                echo "$TS_NAME not running"
443            fi
444            if pgrep $TC_NAME > /dev/null ; then
445                echo "$TC_NAME running as pid `cat $TC_PIDFILE`"; else
446                echo "$TC_NAME not running" ;
447            fi
448            exit $status
449        else
450            echo "This script needs to be ported to this OS"
451            exit 1
452        fi
453        ;;
454    condrestart)
455        if [ "$DISTRIB_ID" = "fedora" -o "$DISTRIB_ID" = "redhat" ]; then
456            if $0 'status'
457            then
458                sh $0 'stop'
459                sh $0 'start'
460            fi
461        else
462            echo "This script needs to be ported to this OS"
463            exit 1
464        fi
465        ;;
466    *)
467        if [ "$DISTRIB_ID" = "ubuntu" -o "$DISTRIB_ID" = "debian" ] ; then
468            echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
469        elif [ "$DISTRIB_ID" = "fedora" -o "$DISTRIB_ID" = "redhat" ]; then
470            echo "Usage: $0 {start|stop|status|restart|condrestart}"
471        elif [ "$DISTRIB_ID" = "gentoo" ]; then
472            echo "Usage: $0 {start|stop|reload|restart|status}"
473        elif [ "$DISTRIB_ID" = "suse" ]; then
474            echo "Usage: $0 {start|stop|restart|status}"
475        elif [ "$DISTRIB_ID" = "Darwin" ]; then
476            echo "Usage: $0 {start|stop|restart|status}"
477        elif [ "$DISTRIB_ID" = "FreeBSD" ]; then
478            echo "Usage: $0 Usage: (start|stop|restart|status)"
479            echo "NB: FreeBSD's ports system installs a rc script in"
480            echo "/usr/local/etc/rc.d/ with native rc.subr(8) features."
481        else
482            echo "This script needs to be ported to this OS"
483            exit 1
484        fi
485        exit 3
486        ;;
487esac
488