source: trunk/third/moira/debian/debathena-moira-update-server.init @ 25055

Revision 25055, 4.0 KB checked in by jdreed, 13 years ago (diff)
Always create the PID file after starting the daemon
  • Property svn:executable set to *
Line 
1#! /bin/sh
2### BEGIN INIT INFO
3# Provides:          debathena-moira-update-server
4# Required-Start:    $local_fs $remote_fs
5# Required-Stop:     $local_fs $remote_fs
6# Default-Start:     2 3 4 5
7# Default-Stop:      0 1 6
8# Short-Description: Moira update_server
9# Description:       The moira update_server program for taking updates from
10#                    moira
11### END INIT INFO
12
13# Author: Evan Broder <broder@mit.edu>
14
15# Do NOT "set -e"
16
17# PATH should only include /usr/* if it runs after the mountnfs.sh script
18PATH=/usr/sbin:/usr/bin:/sbin:/bin
19DESC="Moira update_server"
20NAME=debathena-moira-update-server
21DAEMON=/usr/sbin/update_server
22DAEMON_ARGS=""
23SCRIPTNAME=/etc/init.d/$NAME
24ENABLED=false
25PIDFILE=/var/run/update_server.pid
26
27# Exit if the package is not installed
28[ -x "$DAEMON" ] || exit 0
29
30# Read configuration variable file if it is present
31[ -r /etc/default/$NAME ] && . /etc/default/$NAME
32
33# Exit if the daemon has not been enabled
34[ "$ENABLED" = true ] || exit 0
35
36# Load the VERBOSE setting and other rcS variables
37[ -f /etc/default/rcS ] && . /etc/default/rcS
38
39# Define LSB log_* functions.
40# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
41. /lib/lsb/init-functions
42
43# Function that starts the daemon/service
44#
45do_start()
46{
47        # Return
48        #   0 if daemon has been started
49        #   1 if daemon was already running
50        #   2 if daemon could not be started
51
52        # Remove the following once #849 is fixed and update_server is
53        # capable of writing its own pidfile
54        PID="$(pgrep -f $DAEMON)"
55        if [ -n "$PID" ]; then
56            if ! [ -f "$PIDFILE" ]; then
57                echo "$PID" > $PIDFILE
58            elif [ "$(cat $PIDFILE)" != "$PID" ]; then
59                rm -f $PIDFILE
60                echo "$PID" > $PIDFILE
61            fi
62        fi
63        start-stop-daemon --start --quiet --pidfile $PIDFILE \
64                --exec $DAEMON --test > /dev/null \
65                || return 1
66        start-stop-daemon --start --quiet --pidfile $PIDFILE \
67                --exec $DAEMON -- \
68                $DAEMON_ARGS \
69                || return 2
70        # Remove the following once #849 is fixed and update_server is
71        # capable of writing its own pidfile
72        sleep 1
73        # If we got here, we successfully started the daemon,
74        # so overwrite the PID file
75        rm -f $PIDFILE
76        PID="$(pgrep -f $DAEMON)"
77        echo "$PID" > $PIDFILE
78        # Add code here, if necessary, that waits for the process to be ready
79        # to handle requests from services started subsequently which depend
80        # on this one.  As a last resort, sleep for some time.
81}
82
83#
84# Function that stops the daemon/service
85#
86do_stop()
87{
88        # Return
89        #   0 if daemon has been stopped
90        #   1 if daemon was already stopped
91        #   2 if daemon could not be stopped
92        #   other if a failure occurred
93        start-stop-daemon --stop --quiet --pidfile $PIDFILE
94        RETVAL="$?"
95        [ "$RETVAL" = 2 ] && return 2
96        # Wait for children to finish too if this is a daemon that forks
97        # and if the daemon is only ever run from this initscript.
98        # If the above conditions are not satisfied then add some other code
99        # that waits for the process to drop all resources that could be
100        # needed by services started subsequently.  A last resort is to
101        # sleep for some time.
102        start-stop-daemon --stop --quiet --oknodo --retry=0/10/KILL/5 --exec $DAEMON
103        [ "$?" = 2 ] && return 2
104        return "$RETVAL"
105}
106
107case "$1" in
108  start)
109        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
110        do_start
111        case "$?" in
112                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
113                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
114        esac
115        ;;
116  stop)
117        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
118        do_stop
119        case "$?" in
120                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
121                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
122        esac
123        ;;
124  restart|force-reload)
125        #
126        # If the "reload" option is implemented then remove the
127        # 'force-reload' alias
128        #
129        log_daemon_msg "Restarting $DESC" "$NAME"
130        do_stop
131        case "$?" in
132          0|1)
133                do_start
134                case "$?" in
135                        0) log_end_msg 0 ;;
136                        1) log_end_msg 1 ;; # Old process is still running
137                        *) log_end_msg 1 ;; # Failed to start
138                esac
139                ;;
140          *)
141                # Failed to stop
142                log_end_msg 1
143                ;;
144        esac
145        ;;
146  *)
147        echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
148        exit 3
149        ;;
150esac
151
152:
Note: See TracBrowser for help on using the repository browser.