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