1 Title: Debian init.d boot script example for rsyslog
2 Tags: english, debian, bootsystem
5 <p>If one of the points of switching to a new init system in Debian is
6 <a href="http://thomas.goirand.fr/blog/?p=147">to get rid of huge
7 init.d scripts</a>, I doubt we need to switch away from sysvinit and
8 init.d scripts at all. Here is an example init.d script, ie a rewrite
9 of /etc/init.d/rsyslog:</p>
12 #!/lib/init/init-d-script
15 # Required-Start: $remote_fs $time
16 # Required-Stop: umountnfs $time
17 # X-Stop-After: sendsigs
18 # Default-Start: 2 3 4 5
20 # Short-Description: enhanced syslogd
21 # Description: Rsyslog is an enhanced multi-threaded syslogd.
22 # It is quite compatible to stock sysklogd and can be
23 # used as a drop-in replacement.
25 DESC="enhanced syslogd"
26 DAEMON=/usr/sbin/rsyslogd
29 <p>Pretty minimalistic to me... For the record, the original sysv-rc
30 script was 137 lines, and the above is just 15 lines, most of it meta
33 <p>How to do this, you ask? Well, one create a new script
34 /lib/init/init-d-script looking something like this:
39 # Define LSB log_* functions.
40 # Depend on lsb-base (>= 3.2-14) to ensure that this file is present
41 # and status_of_proc is working.
42 . /lib/lsb/init-functions
45 # Function that starts the daemon/service
51 # 0 if daemon has been started
52 # 1 if daemon was already running
53 # 2 if daemon could not be started
54 start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
56 start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
59 # Add code here, if necessary, that waits for the process to be ready
60 # to handle requests from services started subsequently which depend
61 # on this one. As a last resort, sleep for some time.
65 # Function that stops the daemon/service
70 # 0 if daemon has been stopped
71 # 1 if daemon was already stopped
72 # 2 if daemon could not be stopped
73 # other if a failure occurred
74 start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
76 [ "$RETVAL" = 2 ] && return 2
77 # Wait for children to finish too if this is a daemon that forks
78 # and if the daemon is only ever run from this initscript.
79 # If the above conditions are not satisfied then add some other code
80 # that waits for the process to drop all resources that could be
81 # needed by services started subsequently. A last resort is to
82 # sleep for some time.
83 start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
84 [ "$?" = 2 ] && return 2
85 # Many daemons don't delete their pidfiles when they exit.
91 # Function that sends a SIGHUP to the daemon/service
95 # If the daemon can reload its configuration without
96 # restarting (for example, when it is sent a SIGHUP),
97 # then implement that here.
99 start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
104 scriptbasename="$(basename $1)"
105 echo "SN: $scriptbasename"
106 if [ "$scriptbasename" != "init-d-library" ] ; then
114 NAME=$(basename $DAEMON)
115 PIDFILE=/var/run/$NAME.pid
117 # Exit if the package is not installed
118 #[ -x "$DAEMON" ] || exit 0
120 # Read configuration variable file if it is present
121 [ -r /etc/default/$NAME ] && . /etc/default/$NAME
123 # Load the VERBOSE setting and other rcS variables
128 [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
131 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
132 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
136 [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
139 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
140 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
144 status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
146 #reload|force-reload)
148 # If do_reload() is not implemented then leave this commented out
149 # and leave 'force-reload' as an alias for 'restart'.
151 #log_daemon_msg "Reloading $DESC" "$NAME"
155 restart|force-reload)
157 # If the "reload" option is implemented then remove the
158 # 'force-reload' alias
160 log_daemon_msg "Restarting $DESC" "$NAME"
167 1) log_end_msg 1 ;; # Old process is still running
168 *) log_end_msg 1 ;; # Failed to start
178 echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
186 <p>It is based on /etc/init.d/skeleton, and could be improved quite a
187 lot. I did not really polish the approach, so it might not always
188 work out of the box, but you get the idea. I did not try very hard to
189 optimize it nor make it more robust either.</p>
191 <p>A better argument for switching init system in Debian than reducing
192 the size of init scripts (which is a good thing to do anyway), is to
193 get boot system that is able to handle the kernel events sensibly and
194 robustly, and do not depend on the boot to run sequentially. The boot
195 and the kernel have not behaved sequentially in years.</p>