Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame^] | 1 | #! /bin/sh |
| 2 | ### BEGIN INIT INFO |
| 3 | # Provides: skeleton |
| 4 | # Required-Start: $local_fs |
| 5 | # Should-Start: |
| 6 | # Required-Stop: $local_fs |
| 7 | # Should-Stop: |
| 8 | # Default-Start: 2 3 4 5 |
| 9 | # Default-Stop: 0 1 6 |
| 10 | # Short-Description: Example initscript |
| 11 | # Description: This file should be used to construct scripts to be |
| 12 | # placed in /etc/init.d |
| 13 | ### END INIT INFO |
| 14 | |
| 15 | # The definition of actions: (From LSB 3.1.0) |
| 16 | # start start the service |
| 17 | # stop stop the service |
| 18 | # restart stop and restart the service if the service is already running, |
| 19 | # otherwise start the service |
| 20 | # try-restart restart the service if the service is already running |
| 21 | # reload cause the configuration of the service to be reloaded without |
| 22 | # actually stopping and restarting the service |
| 23 | # force-reload cause the configuration to be reloaded if the service supports |
| 24 | # this, otherwise restart the service if it is running |
| 25 | # status print the current status of the service |
| 26 | |
| 27 | # The start, stop, restart, force-reload, and status actions shall be supported |
| 28 | # by all init scripts; the reload and the try-restart actions are optional |
| 29 | |
| 30 | # Common steps to convert this skeleton into a real init script |
| 31 | # 1) cp skeleton <the_real_name> |
| 32 | # 2) Set DESC and NAME |
| 33 | # 3) Check whether the daemon app is /usr/sbin/$NAME, if not, set it. |
| 34 | # 4) Set DAEMON_ARGS if there is any |
| 35 | # 5) Remove the useless code |
| 36 | |
| 37 | # NOTE: The skeleton doesn't support the daemon which is a script unless the |
| 38 | # pidof supports "-x" option, please see more comments for pidofproc () |
| 39 | # in /etc/init.d/functions |
| 40 | |
| 41 | # PATH should only include /usr/* if it runs after the mountnfs.sh script |
| 42 | PATH=/sbin:/usr/sbin:/bin:/usr/bin |
| 43 | |
| 44 | DESC="skeleton" |
| 45 | NAME="skeleton-test" |
| 46 | DAEMON=/usr/sbin/$NAME |
| 47 | DAEMON_ARGS="" |
| 48 | PIDFILE=/var/run/$NAME.pid |
| 49 | |
| 50 | . /etc/init.d/functions || exit 1 |
| 51 | |
| 52 | # Exit if the package is not installed |
| 53 | [ -x "$DAEMON" ] || exit 0 |
| 54 | |
| 55 | # Read configuration variable file if it is present |
| 56 | [ -r /etc/default/$NAME ] && . /etc/default/$NAME |
| 57 | |
| 58 | # |
| 59 | # Function that starts the daemon/service |
| 60 | # |
| 61 | do_start() { |
| 62 | local status pid |
| 63 | |
| 64 | status=0 |
| 65 | pid=`pidofproc $NAME` || status=$? |
| 66 | case $status in |
| 67 | 0) |
| 68 | echo "$DESC already running ($pid)." |
| 69 | exit 1 |
| 70 | ;; |
| 71 | *) |
| 72 | echo "Starting $DESC ..." |
| 73 | exec $DAEMON $DAEMON_ARGS >/dev/null 2>&1 || status=$? |
| 74 | echo "ERROR: Failed to start $DESC." |
| 75 | exit $status |
| 76 | ;; |
| 77 | esac |
| 78 | |
| 79 | # Add code here, if necessary, that waits for the process to be ready |
| 80 | # to handle requests from services started subsequently which depend |
| 81 | # on this one. As a last resort, sleep for some time. |
| 82 | } |
| 83 | |
| 84 | # |
| 85 | # Function that stops the daemon/service |
| 86 | # |
| 87 | do_stop() { |
| 88 | local pid status |
| 89 | |
| 90 | status=0 |
| 91 | pid=`pidofproc $NAME` || status=$? |
| 92 | case $status in |
| 93 | 0) |
| 94 | # Exit when fail to stop, the kill would complain when fail |
| 95 | kill -s 15 $pid >/dev/null && rm -f $PIDFILE && \ |
| 96 | echo "Stopped $DESC ($pid)." || exit $? |
| 97 | ;; |
| 98 | *) |
| 99 | echo "$DESC is not running; none killed." >&2 |
| 100 | ;; |
| 101 | esac |
| 102 | |
| 103 | # Wait for children to finish too if this is a daemon that forks |
| 104 | # and if the daemon is only ever run from this initscript. |
| 105 | # If the above conditions are not satisfied then add some other code |
| 106 | # that waits for the process to drop all resources that could be |
| 107 | # needed by services started subsequently. A last resort is to |
| 108 | # sleep for some time. |
| 109 | return $status |
| 110 | } |
| 111 | |
| 112 | # |
| 113 | # Function that sends a SIGHUP to the daemon/service |
| 114 | # |
| 115 | do_reload() { |
| 116 | local pid status |
| 117 | |
| 118 | status=0 |
| 119 | # If the daemon can reload its configuration without |
| 120 | # restarting (for example, when it is sent a SIGHUP), |
| 121 | # then implement that here. |
| 122 | pid=`pidofproc $NAME` || status=$? |
| 123 | case $status in |
| 124 | 0) |
| 125 | echo "Reloading $DESC ..." |
| 126 | kill -s 1 $pid || exit $? |
| 127 | ;; |
| 128 | *) |
| 129 | echo "$DESC is not running; none reloaded." >&2 |
| 130 | ;; |
| 131 | esac |
| 132 | exit $status |
| 133 | } |
| 134 | |
| 135 | |
| 136 | # |
| 137 | # Function that shows the daemon/service status |
| 138 | # |
| 139 | status_of_proc () { |
| 140 | local pid status |
| 141 | |
| 142 | status=0 |
| 143 | # pidof output null when no program is running, so no "2>/dev/null". |
| 144 | pid=`pidofproc $NAME` || status=$? |
| 145 | case $status in |
| 146 | 0) |
| 147 | echo "$DESC is running ($pid)." |
| 148 | exit 0 |
| 149 | ;; |
| 150 | *) |
| 151 | echo "$DESC is not running." >&2 |
| 152 | exit $status |
| 153 | ;; |
| 154 | esac |
| 155 | } |
| 156 | |
| 157 | case "$1" in |
| 158 | start) |
| 159 | do_start |
| 160 | ;; |
| 161 | stop) |
| 162 | do_stop || exit $? |
| 163 | ;; |
| 164 | status) |
| 165 | status_of_proc |
| 166 | ;; |
| 167 | restart) |
| 168 | # Always start the service regardless the status of do_stop |
| 169 | do_stop |
| 170 | do_start |
| 171 | ;; |
| 172 | try-restart|force-reload) |
| 173 | # force-reload is the same as reload or try-restart according |
| 174 | # to its definition, the reload is not implemented here, so |
| 175 | # force-reload is the alias of try-restart here, but it should |
| 176 | # be the alias of reload if reload is implemented. |
| 177 | # |
| 178 | # Only start the service when do_stop succeeds |
| 179 | do_stop && do_start |
| 180 | ;; |
| 181 | #reload) |
| 182 | # If the "reload" action is implemented properly, then let the |
| 183 | # force-reload be the alias of reload, and remove it from |
| 184 | # try-restart|force-reload) |
| 185 | # |
| 186 | #do_reload |
| 187 | #;; |
| 188 | *) |
| 189 | echo "Usage: $0 {start|stop|status|restart|try-restart|force-reload}" >&2 |
| 190 | exit 3 |
| 191 | ;; |
| 192 | esac |
| 193 | |