Startup and shutdown of an advanced daemon

Let us add some meat onto the bones of the previous script and make it more complex and featureful. The default methods can do a good job for us, but we may need some of their aspects tweaked. Now we will learn how to tune the default methods to our needs.

		#!/bin/sh 	. /etc/rc.subr	name="mumbled"	rcvar=`set_rcvar`	command="/usr/sbin/${name}"	command_args="mock arguments > /dev/null 2>&1"	pidfile="/var/run/${name}.pid"	required_files="/etc/${name}.conf /usr/share/misc/${name}.rules"	sig_reload="USR1"	start_precmd="${name}_prestart"	stop_postcmd="echo Bye-bye"	extra_commands="reload plugh xyzzy"	plugh_cmd="mumpled_plugh"	xyzzy_cmd="echo 'Nothing happens.'"	mumbled_prestart()	{    	if checkyesno mumbled_smart; then	rc_flags="-o smart ${rc_flags}"	fi	case "$mumbled_mode" in	foo)	rc_flags="-frotz ${rc_flags}"	;;	bar)	rc_flags="-baz ${rc_flags}"	;;	*)	warn "Invalid value for mumbled_mode"	return 1	;;	esac	run_rc_command xyzzy	return 0	}	mumbled_plugh()	{	echo 'A hollow voice says "plugh".'	}	load_rc_config $name	run_rc_command "$1" 	
Additional arguments to $command can be passed in command_args. They will be added to the command line after $mumbled_flags. Since the final command line is passed to eval for its actual execution, input and output redirections can be specified in command_args.

Note: Never include dashed options, like -X or –foo, in command_args. The contents of command_args will appear at the end of the final command line, hence they are likely to follow arguments present in ${name}_flags; but most commands will not recognize dashed options after ordinary arguments. A better way of passing additional options to $command is to add them to the beginning of ${name}_flags. Another way is to modify rc_flags as shown later.

A good-mannered daemon should create a pidfile so that its process can be found more easily and reliably. The variable pidfile, if set, tells rc.subr(8) where it can find the pidfile for its default methods to use.

Note: In fact, rc.subr(8) will also use the pidfile to see if the daemon is already running before starting it. This check can be skipped by using the faststart argument.

If the daemon cannot run unless certain files exist, just list them in required_files, and rc.subr(8) will check that those files do exist before starting the daemon. There also are required_dirs and required_vars for directories and environment variables, respectively. They all are described in detail in rc.subr(8) .

Note: The default method from rc.subr(8) can be forced to skip the prerequisite checks by using forcestart as the argument to the script.

We can customize signals to send to the daemon in case they differ from the well-known ones. In particular, sig_reload specifies the signal that makes the daemon reload its configuration; it is SIGHUP by default. Another signal is sent to stop the daemon process; the default is SIGTERM, but this can be changed by setting sig_stop appropriately.

Note: The signal names should be specified to rc.subr(8) without the SIG prefix, as it is shown in the example. The FreeBSD version of kill(1) can recognize the SIG prefix, but the versions from other OS types may not.


Leave a Comment

Your email address will not be published. Required fields are marked *