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