xref: /trafficserver/rc/trafficserver.in (revision 52f667d5)
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# TS_BASE is offset inside the file system from where the layout starts
101# For standard installations TS_BASE will be empty
102eval TS_BASE="`echo $TS_ROOT | ${ESED} -e 's;@prefix@$;;'`"
103
104TM_NAME=${TM_NAME:-traffic_manager}
105TS_NAME=${TS_NAME:-traffic_server}
106TM_DAEMON=${TM_DAEMON:-$TS_BASE@exp_bindir@/traffic_manager}
107TM_DAEMON_ARGS=""
108TS_DAEMON=${TS_DAEMON:-$TS_BASE@exp_bindir@/traffic_server}
109TS_DAEMON_ARGS=""
110TL_BINARY=${TL_BINARY:-$TS_BASE@exp_bindir@/traffic_ctl}
111TM_PIDFILE=${TM_PIDFILE:-$TS_BASE@exp_runtimedir@/manager.lock}
112TS_PIDFILE=${TS_PIDFILE:-$TS_BASE@exp_runtimedir@/server.lock}
113# number of times to retry check on pid lock file
114PIDFILE_CHECK_RETRIES=${PIDFILE_CHECK_RETRIES:-30}
115# stdout file of executable
116STDOUTLOG=${STDOUTLOG:-$TS_BASE@exp_logdir@/traffic.out}
117# stderr file of executable
118STDERRLOG=${STDERRLOG:-$TS_BASE@exp_logdir@/traffic.out}
119
120if [ -d /etc/rc.d/init.d ]; then
121    SCRIPTNAME=/etc/rc.d/init.d/$NAME # Fedora
122elif [ -d /etc/init.d ]; then
123    SCRIPTNAME=/etc/init.d/$NAME # Ubuntu
124elif [ "$DISTRIB_ID" = "Darwin" ]; then
125    SCRIPTNAME=$0 # Darwin
126elif [ "$DISTRIB_ID" = "FreeBSD" ]; then
127    SCRIPTNAME=$0 # FreeBSD
128else
129    echo "This script needs to be ported to this OS"
130    exit 1
131fi
132
133# Exit if the package can not be found
134if [ ! -x "$TS_DAEMON" ]; then
135    echo "Error: Can not find $TS_DAEMON using TS_ROOT=$TS_ROOT"
136    echo "       Please set TS_ROOT to point to base installation."
137    exit 0
138fi
139
140# Ubuntu: Read configuration variable file if it is present
141test -r /etc/default/$NAME && . /etc/default/$NAME
142
143# RHEL/Fedora: Read configuration variable file if it is present
144test -r /etc/sysconfig/$NAME && . /etc/sysconfig/$NAME
145
146# Gentoo: Read configuration variable file if it is present
147test -r /etc/conf.d/$NAME && . /etc/conf.d/$NAME
148
149# Ubuntu: Load the VERBOSE setting and other rcS variables
150test -f /etc/default/rcS && . /etc/default/rcS
151
152# Suse: load rc.status
153test -f /etc/rc.status && . /etc/rc.status
154
155# Fedora: make avail helper functions 'action', 'killproc', 'status', etc
156test -f /etc/rc.d/init.d/functions && . /etc/rc.d/init.d/functions
157
158# Gentoo: make avail helper function 'ebegin', 'eend', etc
159test -f /etc/init.d/functions.sh && . /etc/init.d/functions.sh
160
161# Ubuntu: Define LSB log_* functions.
162#         Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
163test -f /lib/lsb/init-functions && . /lib/lsb/init-functions
164
165# FreeBSD
166# NB: FreeBSD's ports system installs a run script in /usr/local/etc/rc.d/
167# with native OS rc.subr(8) features.
168test -f /etc/rc.subr && . /etc/rc.subr
169
170if [ ! -d $TS_BASE@exp_runtimedir@ ]
171then
172	mkdir -p $TS_BASE@exp_runtimedir@
173	chown @pkgsysuser@:@pkgsysgroup@ $TS_BASE@exp_runtimedir@
174fi
175
176#
177# Fedora:
178#  launches $* in the background
179#  and checks if program creates its own pid lock file
180#  redirects stdout and stderr to files
181#  this function is meant to substitute 'daemon'
182#  because 'daemon' expects $1 to fork itself
183forkdaemon()
184{
185    local i=0
186
187    # launch in background, i.e. fork
188    # and redirect stdout and stderr to files
189    $* --bind_stdout $STDOUTLOG --bind_stderr $STDERRLOG >> $STDOUTLOG 2>> $STDERRLOG &
190
191    while (( $i < $PIDFILE_CHECK_RETRIES ))
192    do
193        # check for regular file and size greater than 0
194        if [[ -f $TM_PIDFILE ]] && [[ -s $TM_PIDFILE ]]
195        then
196            success
197            return 0
198        fi
199
200        sleep 1  # zzz for 1 second
201        (( i++ ))
202
203    done
204
205    failure
206    return 1
207}
208
209
210#
211# Ubuntu: Function that starts the daemon/service
212#
213do_start()
214{
215  # Return
216  #   0 if daemon has been started
217  #   1 if daemon was already running
218  #   2 if daemon could not be started
219    start-stop-daemon --start --quiet --pidfile $TM_PIDFILE --exec $TM_DAEMON --test > /dev/null \
220        || return 1
221    start-stop-daemon --start --background --quiet --pidfile $TM_PIDFILE --exec $TM_DAEMON -- \
222        $TM_DAEMON_ARGS \
223        || return 2
224  # Add code here, if necessary, that waits for the process to be ready
225  # to handle requests from services started subsequently which depend
226  # on this one.  As a last resort, sleep for some time.
227    sleep 1
228    test -f "$TM_PIDFILE" || return 2
229}
230
231#
232# Ubuntu: Function that stops the daemon/service
233#
234do_stop()
235{
236  # Return
237  #   0 if daemon has been stopped
238  #   1 if daemon was already stopped
239  #   2 if daemon could not be stopped
240  #   other if a failure occurred
241    start-stop-daemon --stop --quiet --retry=QUIT/30/KILL/5 --pidfile $TM_PIDFILE --name $TM_NAME
242    RETVAL="$?"
243    test "$RETVAL" != 0 && return $RETVAL
244  # Wait for children to finish too if this is a daemon that forks
245  # and if the daemon is only ever run from this initscript.
246  # If the above conditions are not satisfied then add some other code
247  # that waits for the process to drop all resources that could be
248  # needed by services started subsequently.  A last resort is to
249  # sleep for some time.
250    start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $TM_DAEMON
251    RETVAL="$?"
252    test "$RETVAL" != 0 && return $RETVAL
253  # Need to stop the TM and TS also
254    start-stop-daemon --stop --quiet --oknodo --retry=QUIT/30/KILL/5 --pidfile $TM_PIDFILE --name $TM_NAME
255    RETVAL="$?"
256    test "$RETVAL" != 0 && return $RETVAL
257    start-stop-daemon --stop --quiet --oknodo --retry=QUIT/30/KILL/5 --pidfile $TS_PIDFILE --name $TS_NAME
258    RETVAL="$?"
259    test "$RETVAL" != 0 && return $RETVAL
260  # Many daemons don't delete their pidfiles when they exit.
261    rm -f $TM_PIDFILE
262    rm -f $TM_PIDFILE
263    rm -f $TS_PIDFILE
264    return "$RETVAL"
265}
266
267# main
268case "$1" in
269    start)
270        if [ "$DISTRIB_ID" = "ubuntu" -o "$DISTRIB_ID" = "debian" ] ; then
271            test "x$VERBOSE" != "xno" && log_daemon_msg "Starting ${TS_PACKAGE_NAME}" "$NAME"
272            retval=0
273            do_start
274            test "$?" -ne 0 -a "$?" -ne 1 && retval=1
275            test "x$VERBOSE" != "xno" && log_end_msg "$retval"
276            exit "$retval"
277        elif [ "$DISTRIB_ID" = "gentoo" ]; then
278	    ebegin "Starting ${TS_PACKAGE_NAME}"
279	    do_start
280	    eend $?
281        elif [ "$DISTRIB_ID" = "fedora" -o "$DISTRIB_ID" = "redhat" ]; then
282            action "Starting ${TS_PACKAGE_NAME}:" forkdaemon $TM_DAEMON $TM_DAEMON_ARGS
283        elif [ "$DISTRIB_ID" = "suse" ]; then
284            echo -n "Starting ${TS_PACKAGE_NAME}"
285            startproc -p $TM_PIDFILE $TM_DAEMON $TM_DAEMON_ARGS
286            rc_status -v
287        elif [ "$DISTRIB_ID" = "Darwin" ]; then
288            echo "Starting ${TS_PACKAGE_NAME}"
289            launchctl bsexec / launchctl list $TM_NAME > /dev/null 2>&1 && exit 0
290            launchctl bsexec / launchctl submit -l $TM_NAME -p $TM_DAEMON -o $STDOUTLOG -e $STDERRLOG -- $TM_DAEMON_ARGS
291        elif [ "$DISTRIB_ID" = "FreeBSD" ]; then
292            echo "Starting ${TS_PACKAGE_NAME}"
293            name="$TM_NAME"
294            command="/usr/sbin/daemon"
295            command_args="$TM_DAEMON $TM_DAEMON_ARGS"
296            pidfile="$TM_PIDFILE"
297            run_rc_command "$1"
298        else
299            echo "This script needs to be ported to this OS"
300            exit 1
301        fi
302        ;;
303    stop)
304        if [ "$DISTRIB_ID" = "ubuntu" -o "$DISTRIB_ID" = "debian" ] ; then
305            test "x$VERBOSE" != "xno" && log_daemon_msg "Stopping ${TS_PACKAGE_NAME}" "$NAME"
306            retval=0
307            do_stop
308            test "$?" -ne 0 -a "$?" -ne 1 && retval=1
309            test "x$VERBOSE" != "xno" && log_end_msg "$retval"
310            exit "$retval"
311        elif [ "$DISTRIB_ID" = "fedora" -o "$DISTRIB_ID" = "redhat" ]; then
312            action "Stopping ${TM_NAME}:" killproc -p $TM_PIDFILE -d 35 $TM_DAEMON
313            action "Stopping ${TS_NAME}:" killproc -p $TS_PIDFILE -d 35 $TS_DAEMON
314        elif [ "$DISTRIB_ID" = "gentoo" ]; then
315	    ebegin "Starting ${TS_PACKAGE_NAME}"
316	    do_stop
317	    eend $?
318        elif [ "$DISTRIB_ID" = "suse" ]; then
319            echo -n "Stopping ${TM_NAME}"
320            killproc -p $TM_PIDFILE $TM_DAEMON
321            rc_status -v
322            echo -n "Stopping ${TS_NAME}"
323            killproc -p $TS_PIDFILE $TS_DAEMON
324            rc_status -v
325        elif [ "$DISTRIB_ID" = "Darwin" ]; then
326            echo "Stopping ${TS_PACKAGE_NAME}"
327            launchctl bsexec / launchctl list $TM_NAME > /dev/null 2>&1 || exit 0
328            echo "Stopping ${TM_NAME}"
329            launchctl bsexec / launchctl remove ${TM_NAME}
330            rm -f ${TM_PIDFILE}
331            echo "Stopping ${TS_NAME}"
332            kill $(cat $TS_PIDFILE)
333            rm -f ${TS_PIDFILE}
334        elif [ "$DISTRIB_ID" = "FreeBSD" ]; then
335            echo "Stopping ${TS_PACKAGE_NAME}"
336            if [ -e "$TM_PIDFILE" ]; then
337                kill $(cat $TM_PIDFILE)
338                rm -f ${TM_PIDFILE}
339            fi
340        else
341            echo "This script needs to be ported to this OS"
342            exit 1
343        fi
344        ;;
345    reload|force-reload)
346        if [ "$DISTRIB_ID" = "ubuntu" -o "$DISTRIB_ID" = "debian" ] ; then
347            test "x$VERBOSE" != "xno" && log_daemon_msg "Reloading ${TS_PACKAGE_NAME}" "$NAME"
348            retval=0
349            $TL_BINARY config reload
350            test "$?" -ne 0 -a "$?" -ne 1 && retval=1
351            test "x$VERBOSE" != "xno" && log_end_msg "$retval"
352            exit "$retval"
353        elif [ "$DISTRIB_ID" = "fedora" -o "$DISTRIB_ID" = "redhat" ]; then
354            action "Reloading ${NAME}:" $TL_BINARY config reload
355        elif [ "$DISTRIB_ID" = "gentoo" ]; then
356	    ebegin "Reloading ${NAME}"
357            $TL_BINARY config reload
358	    eend $?
359        elif [ "$DISTRIB_ID" = "suse" ]; then
360            echo -n "Reloading ${NAME}"
361            $TL_BINARY config reload
362            rc_status -v
363        elif [ "$DISTRIB_ID" = "Darwin" ]; then
364            echo "Reloading ${NAME}"
365            $TL_BINARY config reload
366        elif [ "$DISTRIB_ID" = "FreeBSD" ]; then
367            echo "Reloading ${NAME}"
368            $TL_BINARY config reload
369        else
370            echo "This script needs to be ported to this OS"
371            exit 1
372        fi
373        ;;
374    restart)
375        if [ "$DISTRIB_ID" = "ubuntu" -o "$DISTRIB_ID" = "debian" ] ; then
376      #
377      # If the "reload" option is implemented then remove the
378      # 'force-reload' alias
379      #
380            log_daemon_msg "Restarting ${TS_PACKAGE_NAME}" "$NAME"
381            do_stop
382            case "$?" in
383                0|1)
384                    sleep $SLEEP_TIME
385                    do_start
386                    case "$?" in
387                        0) log_end_msg 0 ;;
388                        1) log_end_msg 0 ;; # Daemons were already stopped
389                        *) log_end_msg 1 ;; # Failed to start
390                    esac
391                    ;;
392                *)
393            # Failed to stop
394                    log_end_msg 1
395                    ;;
396            esac
397        else
398      # This is the typical process for restart
399            sh $0 'stop'
400            sleep $SLEEP_TIME
401            sh $0 'start'
402        fi
403        ;;
404    status)
405        if [ "$DISTRIB_ID" = "fedora" -o "$DISTRIB_ID" = "redhat" ]; then
406            status -p $TM_PIDFILE $TM_NAME
407        elif [ "$DISTRIB_ID" = "ubuntu" -o "$DISTRIB_ID" = "debian" ] ; then
408            status_of_proc "$TM_DAEMON" "$TM_NAME" -p "$TM_PIDFILE" && exit 0 || exit $?
409        elif [  "$DISTRIB_ID" = "suse" ]; then
410            echo -n "Checking for service ${DM}: "
411            checkproc -p $TM_PIDFILE $TM_NAME
412            rc_status -v
413        elif [ "$DISTRIB_ID" = "Darwin" ]; then
414            /bin/echo -n "${TS_PACKAGE_NAME} is "
415            launchctl bsexec / launchctl list $TM_NAME > /dev/null 2>&1
416            status=$?
417            [ $status -eq 0 ] || /bin/echo -n "not "
418            echo "running."
419        elif [ "$DISTRIB_ID" = "FreeBSD" -o "$DISTRIB_ID" = "gentoo" ]; then
420            if pgrep $TM_NAME > /dev/null ; then
421                echo "$TM_NAME running as pid `cat $TM_PIDFILE`" ; else
422                echo "$TM_NAME not running"
423            fi
424            if pgrep $TS_NAME > /dev/null ; then
425                echo "$TS_NAME running as pid `cat $TS_PIDFILE`"; else
426                echo "$TS_NAME not running"
427            fi
428            exit $status
429        else
430            echo "This script needs to be ported to this OS"
431            exit 1
432        fi
433        ;;
434    condrestart)
435        if [ "$DISTRIB_ID" = "fedora" -o "$DISTRIB_ID" = "redhat" ]; then
436            if $0 'status'
437            then
438                sh $0 'stop'
439                sh $0 'start'
440            fi
441        else
442            echo "This script needs to be ported to this OS"
443            exit 1
444        fi
445        ;;
446    *)
447        if [ "$DISTRIB_ID" = "ubuntu" -o "$DISTRIB_ID" = "debian" ] ; then
448            echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
449        elif [ "$DISTRIB_ID" = "fedora" -o "$DISTRIB_ID" = "redhat" ]; then
450            echo "Usage: $0 {start|stop|status|restart|condrestart}"
451        elif [ "$DISTRIB_ID" = "gentoo" ]; then
452            echo "Usage: $0 {start|stop|reload|restart|status}"
453        elif [ "$DISTRIB_ID" = "suse" ]; then
454            echo "Usage: $0 {start|stop|restart|status}"
455        elif [ "$DISTRIB_ID" = "Darwin" ]; then
456            echo "Usage: $0 {start|stop|restart|status}"
457        elif [ "$DISTRIB_ID" = "FreeBSD" ]; then
458            echo "Usage: $0 Usage: (start|stop|restart|status)"
459            echo "NB: FreeBSD's ports system installs a rc script in"
460            echo "/usr/local/etc/rc.d/ with native rc.subr(8) features."
461        else
462            echo "This script needs to be ported to this OS"
463            exit 1
464        fi
465        exit 3
466        ;;
467esac
468