xref: /trafficserver/rc/trafficserver.in (revision 589d8013)
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/redhat-release ]; then
57    DISTRIB_ID="redhat" # also exists on Fedora
58elif [ -e /etc/lsb-release ]; then
59    DISTRIB_ID="ubuntu"
60elif [ -e /etc/debian_version ]; then
61    DISTRIB_ID="debian" # also exists on Ubuntu
62elif [ -e /etc/slackware-version ]; then
63    DISTRIB_ID="slackware"
64elif [ "$UNAME_S" = "Darwin" ]; then
65    DISTRIB_ID="Darwin"
66elif [ "$UNAME_S" = "FreeBSD" ]; then
67    DISTRIB_ID="FreeBSD"
68fi
69
70# We might want to move over to use /etc/system-release for more distros?
71# I know at least Fedora supports it as well.
72if [ "" = "$DISTRIB_ID" -a -e /etc/system-release ]; then
73    case $(cat /etc/system-release) in
74	*Amazon*)
75	    DISTRIB_ID="redhat" # Amazon AMI is mostly like RHEL
76	    ;;
77	*)
78	    ;;
79    esac
80fi
81
82# /usr/bin/lsb_release -rd # Ubuntu release & description info
83
84HEAD=/usr/bin/head
85test -x $HEAD || HEAD=head
86ESED=/usr/bin/sed
87test -x $ESED || ESED=sed
88
89# Ubuntu: Do NOT "set -e"
90
91#Ubuntu:PATH should only include /usr/* if it runs after the mountnfs.sh script
92PATH=/usr/sbin:/usr/bin:/sbin:/bin
93DESC="start/stop Traffic Server"
94NAME=trafficserver
95SLEEP_TIME=5
96TS_PREFIX="@prefix@"
97
98TS_ROOT=${TS_ROOT:-$TS_PREFIX}
99# TS_BASE is offset inside the file system from where the layout starts
100# For standard installations TS_BASE will be empty
101eval TS_BASE="`echo $TS_ROOT | ${ESED} -e 's;@prefix@$;;'`"
102
103TC_NAME=${TC_NAME:-traffic_cop}
104TM_NAME=${TM_NAME:-traffic_manager}
105TS_NAME=${TS_NAME:-traffic_server}
106TC_DAEMON=${TC_DAEMON:-$TS_BASE@exp_bindir@/traffic_cop}
107TC_DAEMON_ARGS=""
108TM_DAEMON=${TM_DAEMON:-$TS_BASE@exp_bindir@/traffic_manager}
109TM_DAEMON_ARGS=""
110TS_DAEMON=${TS_DAEMON:-$TS_BASE@exp_bindir@/traffic_server}
111TS_DAEMON_ARGS=""
112TL_BINARY=${TL_BINARY:-$TS_BASE@exp_bindir@/traffic_line}
113TC_PIDFILE=${TC_PIDFILE:-$TS_BASE@exp_runtimedir@/cop.lock}
114TM_PIDFILE=${TM_PIDFILE:-$TS_BASE@exp_runtimedir@/manager.lock}
115TS_PIDFILE=${TS_PIDFILE:-$TS_BASE@exp_runtimedir@/server.lock}
116# number of times to retry check on pid lock file
117PIDFILE_CHECK_RETRIES=${PIDFILE_CHECK_RETRIES:-30}
118# stdout file of executable
119STDOUTLOG=${STDOUTLOG:-$TS_BASE@exp_logdir@/traffic_server.stdout}
120# stderr file of executable
121STDERRLOG=${STDERRLOG:-$TS_BASE@exp_logdir@/traffic_server.stderr}
122
123if [ -d /etc/rc.d/init.d ]; then
124    SCRIPTNAME=/etc/rc.d/init.d/$NAME # Fedora
125elif [ -d /etc/init.d ]; then
126    SCRIPTNAME=/etc/init.d/$NAME # Ubuntu
127elif [ "$DISTRIB_ID" = "Darwin" ]; then
128    SCRIPTNAME=$0 # Darwin
129elif [ "$DISTRIB_ID" = "FreeBSD" ]; then
130    SCRIPTNAME=$0 # FreeBSD
131else
132    echo "This script needs to be ported to this OS"
133    exit 1
134fi
135
136# Exit if the package can not be found
137if [ ! -x "$TS_DAEMON" ]; then
138    echo "Error: Can not find $TS_DAEMON using TS_ROOT=$TS_ROOT"
139    echo "       Please set TS_ROOT to point to base installation."
140    exit 0
141fi
142
143# Ubuntu: Read configuration variable file if it is present
144test -r /etc/default/$NAME && . /etc/default/$NAME
145
146# RHEL/Fedora: Read configuration variable file if it is present
147test -r /etc/sysconfig/$NAME && . /etc/sysconfig/$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# Ubuntu: Define LSB log_* functions.
159#         Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
160test -f /lib/lsb/init-functions && . /lib/lsb/init-functions
161
162# FreeBSD
163# NB: FreeBSD's ports system installs a run script in /usr/local/etc/rc.d/
164# with native OS rc.subr(8) features.
165test -f /etc/rc.subr && . /etc/rc.subr
166
167if [ ! -d $TS_BASE@exp_runtimedir@ ]
168then
169	mkdir -p $TS_BASE@exp_runtimedir@
170	chown @pkgsysuser@:@pkgsysgroup@ $TS_BASE@exp_runtimedir@
171fi
172
173#
174# Fedora:
175#  launches $* in the background
176#  and checks if program creates its own pid lock file
177#  redirects stdout and stderr to files
178#  this function is meant to substitute 'daemon'
179#  because 'daemon' expects $1 to fork itself
180forkdaemon()
181{
182    local i=0
183
184    # launch in background, i.e. fork
185    # and redirect stdout and stderr to files
186    $* >> $STDOUTLOG 2>> $STDERRLOG &
187
188    while (( $i < $PIDFILE_CHECK_RETRIES ))
189    do
190        # check for regular file and size greater than 0
191        if [[ -f $TC_PIDFILE ]] && [[ -s $TC_PIDFILE ]]
192        then
193            success
194            return 0
195        fi
196
197        sleep 1  # zzz for 1 second
198        (( i++ ))
199
200    done
201
202    failure
203    return 1
204}
205
206
207#
208# Ubuntu: Function that starts the daemon/service
209#
210do_start()
211{
212  # Return
213  #   0 if daemon has been started
214  #   1 if daemon was already running
215  #   2 if daemon could not be started
216    start-stop-daemon --start --quiet --pidfile $TC_PIDFILE --exec $TC_DAEMON --test > /dev/null \
217        || return 1
218    start-stop-daemon --start --background --quiet --pidfile $TC_PIDFILE --exec $TC_DAEMON -- \
219        $TC_DAEMON_ARGS \
220        || return 2
221  # Add code here, if necessary, that waits for the process to be ready
222  # to handle requests from services started subsequently which depend
223  # on this one.  As a last resort, sleep for some time.
224    sleep 1
225    test -f "$TC_PIDFILE" || return 2
226}
227
228#
229# Ubuntu: Function that stops the daemon/service
230#
231do_stop()
232{
233  # Return
234  #   0 if daemon has been stopped
235  #   1 if daemon was already stopped
236  #   2 if daemon could not be stopped
237  #   other if a failure occurred
238    start-stop-daemon --stop --quiet --retry=QUIT/30/KILL/5 --pidfile $TC_PIDFILE --name $TC_NAME
239    RETVAL="$?"
240    test "$RETVAL" != 0 && return $RETVAL
241  # Wait for children to finish too if this is a daemon that forks
242  # and if the daemon is only ever run from this initscript.
243  # If the above conditions are not satisfied then add some other code
244  # that waits for the process to drop all resources that could be
245  # needed by services started subsequently.  A last resort is to
246  # sleep for some time.
247    start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $TC_DAEMON
248    RETVAL="$?"
249    test "$RETVAL" != 0 && return $RETVAL
250  # Need to stop the TM and TS also
251    start-stop-daemon --stop --quiet --oknodo --retry=QUIT/30/KILL/5 --pidfile $TM_PIDFILE --name $TM_NAME
252    RETVAL="$?"
253    test "$RETVAL" != 0 && return $RETVAL
254    start-stop-daemon --stop --quiet --oknodo --retry=QUIT/30/KILL/5 --pidfile $TS_PIDFILE --name $TS_NAME
255    RETVAL="$?"
256    test "$RETVAL" != 0 && return $RETVAL
257  # Many daemons don't delete their pidfiles when they exit.
258    rm -f $TC_PIDFILE
259    rm -f $TM_PIDFILE
260    rm -f $TS_PIDFILE
261    return "$RETVAL"
262}
263
264# main
265case "$1" in
266    start)
267        if [ "$DISTRIB_ID" = "ubuntu" -o "$DISTRIB_ID" = "debian" ] ; then
268            test "x$VERBOSE" != "xno" && log_daemon_msg "Starting ${TS_PACKAGE_NAME}" "$NAME"
269            retval=0
270            do_start
271            test "$?" -ne 0 -a "$?" -ne 1 && retval=1
272            test "x$VERBOSE" != "xno" && log_end_msg "$retval"
273            exit "$retval"
274        elif [ "$DISTRIB_ID" = "fedora" -o "$DISTRIB_ID" = "redhat" ]; then
275            action "Starting ${TS_PACKAGE_NAME}:" forkdaemon $TC_DAEMON $TC_DAEMON_ARGS
276        elif [ "$DISTRIB_ID" = "suse" ]; then
277            echo -n "Starting ${TS_PACKAGE_NAME}"
278            startproc -p $TC_PIDFILE $TC_DAEMON $TC_DAEMON_ARGS
279            rc_status -v
280        elif [ "$DISTRIB_ID" = "Darwin" ]; then
281            echo "Starting ${TS_PACKAGE_NAME}"
282            launchctl bsexec / launchctl list $TC_NAME > /dev/null 2>&1 && exit 0
283            launchctl bsexec / launchctl submit -l $TC_NAME -p $TC_DAEMON -o $STDOUTLOG -e $STDERRLOG -- $TC_DAEMON_ARGS
284        elif [ "$DISTRIB_ID" = "FreeBSD" ]; then
285            echo "Starting ${TS_PACKAGE_NAME}"
286            name="$TC_NAME"
287            command="/usr/sbin/daemon"
288            command_args="$TC_DAEMON $TC_DAEMON_ARGS"
289            pidfile="$TC_PIDFILE"
290            run_rc_command "$1"
291        else
292            echo "This script needs to be ported to this OS"
293            exit 1
294        fi
295        ;;
296    stop)
297        if [ "$DISTRIB_ID" = "ubuntu" -o "$DISTRIB_ID" = "debian" ] ; then
298            test "x$VERBOSE" != "xno" && log_daemon_msg "Stopping ${TS_PACKAGE_NAME}" "$NAME"
299            retval=0
300            do_stop
301            test "$?" -ne 0 -a "$?" -ne 1 && retval=1
302            test "x$VERBOSE" != "xno" && log_end_msg "$retval"
303            exit "$retval"
304        elif [ "$DISTRIB_ID" = "fedora" -o "$DISTRIB_ID" = "redhat" ]; then
305            action "Stopping ${TC_NAME}:" killproc -p $TC_PIDFILE $TC_DAEMON
306            action "Stopping ${TM_NAME}:" killproc -p $TM_PIDFILE $TM_DAEMON
307            action "Stopping ${TS_NAME}:" killproc -p $TS_PIDFILE $TS_DAEMON
308        elif [ "$DISTRIB_ID" = "suse" ]; then
309            echo -n "Stopping ${TC_NAME}"
310            killproc -p $TC_PIDFILE $TC_DAEMON
311            rc_status -v
312            echo -n "Stopping ${TM_NAME}"
313            killproc -p $TM_PIDFILE $TM_DAEMON
314            rc_status -v
315            echo -n "Stopping ${TS_NAME}"
316            killproc -p $TS_PIDFILE $TS_DAEMON
317            rc_status -v
318        elif [ "$DISTRIB_ID" = "Darwin" ]; then
319            echo "Stopping ${TS_PACKAGE_NAME}"
320            launchctl bsexec / launchctl list $TC_NAME > /dev/null 2>&1 || exit 0
321            echo "Stopping ${TC_NAME}"
322            launchctl bsexec / launchctl remove ${TC_NAME}
323            rm -f ${TC_PIDFILE}
324            echo "Stopping ${TM_NAME}"
325            kill $(cat $TM_PIDFILE)
326            rm -f ${TM_PIDFILE}
327            echo "Stopping ${TS_NAME}"
328            kill $(cat $TS_PIDFILE)
329            rm -f ${TS_PIDFILE}
330        elif [ "$DISTRIB_ID" = "FreeBSD" ]; then
331            echo "Stopping ${TS_PACKAGE_NAME}"
332            if [ -e "$TC_PIDFILE" ]; then
333                kill $(cat $TC_PIDFILE)
334                rm -f ${TC_PIDFILE}
335            fi
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 -x
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 -x
355        elif [ "$DISTRIB_ID" = "suse" ]; then
356            echo -n "Reloading ${NAME}"
357            $TL_BINARY -x
358            rc_status -v
359        elif [ "$DISTRIB_ID" = "Darwin" ]; then
360            echo "Reloading ${NAME}"
361            $TL_BINARY -x
362        elif [ "$DISTRIB_ID" = "FreeBSD" ]; then
363            echo "Reloading ${NAME}"
364            $TL_BINARY -x
365        else
366            echo "This script needs to be ported to this OS"
367            exit 1
368        fi
369        ;;
370    restart)
371        if [ "$DISTRIB_ID" = "ubuntu" -o "$DISTRIB_ID" = "debian" ] ; then
372      #
373      # If the "reload" option is implemented then remove the
374      # 'force-reload' alias
375      #
376            log_daemon_msg "Restarting ${TS_PACKAGE_NAME}" "$NAME"
377            do_stop
378            case "$?" in
379                0|1)
380                    sleep $SLEEP_TIME
381                    do_start
382                    case "$?" in
383                        0) log_end_msg 0 ;;
384                        1) log_end_msg 0 ;; # Daemons were already stopped
385                        *) log_end_msg 1 ;; # Failed to start
386                    esac
387                    ;;
388                *)
389            # Failed to stop
390                    log_end_msg 1
391                    ;;
392            esac
393        else
394      # This is the typical process for restart
395            sh $0 'stop'
396            sleep $SLEEP_TIME
397            sh $0 'start'
398        fi
399        ;;
400    status)
401        if [ "$DISTRIB_ID" = "fedora" -o "$DISTRIB_ID" = "redhat" ]; then
402            status -p $TC_PIDFILE $TC_NAME
403        elif [ "$DISTRIB_ID" = "ubuntu" -o "$DISTRIB_ID" = "debian" ] ; then
404            status_of_proc "$TC_DAEMON" "$TC_NAME" -p "$TC_PIDFILE" && exit 0 || exit $?
405        elif [  "$DISTRIB_ID" = "suse" ]; then
406            echo -n "Checking for service ${DM}: "
407            checkproc -p $TC_PIDFILE $TC_NAME
408            rc_status -v
409        elif [ "$DISTRIB_ID" = "Darwin" ]; then
410            /bin/echo -n "${TS_PACKAGE_NAME} is "
411            launchctl bsexec / launchctl list $TC_NAME > /dev/null 2>&1
412            status=$?
413            [ $status -eq 0 ] || /bin/echo -n "not "
414            echo "running."
415        elif [ "$DISTRIB_ID" = "FreeBSD" ]; then
416            if pgrep $TM_NAME > /dev/null ; then
417                echo "$TM_NAME running as pid `cat $TM_PIDFILE`" ; else
418                echo "$TM_NAME not running"
419            fi
420            if pgrep $TS_NAME > /dev/null ; then
421                echo "$TS_NAME running as pid `cat $TS_PIDFILE`"; else
422                echo "$TS_NAME not running"
423            fi
424            if pgrep $TC_NAME > /dev/null ; then
425                echo "$TC_NAME running as pid `cat $TC_PIDFILE`"; else
426                echo "$TC_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" = "suse" ]; then
452            echo "Usage: $0 {start|stop|restart|status}"
453        elif [ "$DISTRIB_ID" = "Darwin" ]; then
454            echo "Usage: $0 {start|stop|restart|status}"
455        elif [ "$DISTRIB_ID" = "FreeBSD" ]; then
456            echo "Usage: $0 Usage: (start|stop|restart|status)"
457            echo "NB: FreeBSD's ports system installs a rc script in"
458            echo "/usr/local/etc/rc.d/ with native rc.subr(8) features."
459        else
460            echo "This script needs to be ported to this OS"
461            exit 1
462        fi
463        exit 3
464        ;;
465esac
466