# Multiple daemon/profile rc.d script



## tuaris (Jun 25, 2014)

I have successfully been able to write up an rc.d script to manage multiple instances of a Python daemon. I used www/fcgiwrap as a reference. 

In rc.conf I have:

```
tidepool_enable="YES"
tidepool_profiles="tekpool joulepool"
```

The rc.d script is as follows:

```
#!/bin/sh

# $FreeBSD$
#
# PROVIDE: tidepool
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# tidepool_enable (bool):	Set to NO by default.
#				Set it to YES to enable tidepool.
# tidepool_confdir (path):	Set to /usr/local/etc/tidepool
#				by default.
# tidepool_poolname (name):	Set to tidepool
#				by default.

. /etc/rc.subr

name=tidepool
rcvar=${name}_enable

load_rc_config ${name}

: ${tidepool_enable:="NO"}
: ${tidepool_confdir="/usr/local/etc/tidepool"}
: ${tidepool_poolname="tidepool"}

# This handles profile specific vars.
if [ -n "$2" ]; then
	profile="$2"
	if [ -n "${tidepool_profiles}" ]; then
		eval tidepool_enable="\${tidepool_${profile}_enable:-${tidepool_enable}}"
		tidepool_poolname=${profile}
	else
		echo "$0: extra argument ignored"
	fi
else
	if [ -n "${tidepool_profiles}" -a -n "$1" ]; then
		for profile in ${tidepool_profiles}; do
			echo "===> ${name} profile: ${profile}"
			/usr/local/etc/rc.d/${name} $1 ${profile}
			retcode="$?"
			if [ "0${retcode}" -ne 0 ]; then
				failed="${profile} (${retcode}) ${failed:-}"
			else
				success="${profile} ${success:-}"
			fi
		done
		# It exits so that non-profile rc.d is not started when there are profiles.
		exit 0
	fi
fi

tidepool_chdir=/usr/local/${name}

tacfile=${tidepool_confdir}/tacs/${tidepool_poolname}.tac
pidfile=/var/run/${tidepool_poolname}.pid
procname=${tidepool_poolname}d
command="/usr/local/bin/twistd"
required_files=${tacfile}
command_args="-y ${tacfile} --pidfile ${pidfile} -l /var/log/${tidepool_poolname}.log"

run_rc_command "$1"
```

It seems to work. For example, by a specific profile:
`root@pools01:~ # service tidepool start tekpool`

```
Starting tidepool.
root@pools01:~ # service tidepool status tekpool
tidepool is running as pid 8438.
root@pools01:~ # service tidepool stop tekpool
Stopping tidepool.
```

It also starts all profiles without specifying a profile name:
`root@pools01:~ # service tidepool start`

```
===> tidepool profile: tekpool
Starting tidepool.
===> tidepool profile: joulepool
Starting tidepool.
root@pools01:~ # service tidepool status
===> tidepool profile: tekpool
tidepool is running as pid 8481.
===> tidepool profile: joulepool
tidepool is running as pid 8487.
root@pools01:~ # service tidepool stop
===> tidepool profile: tekpool
Stopping tidepool.
===> tidepool profile: joulepool
Stopping tidepool.
```

If you notice above each profile always uses the same status "Starting tidepool" for example.  How can I make it so it uses the profile name so it looks like "Starting tekpool"?

Secondly, I was hoping some one could explain the following line in the script:

```
eval tidepool_enable="\${tidepool_${profile}_enable:-${tidepool_enable}}"
```

Am I correct in assuming that this will allow me to disable specific profiles in rc.conf?

```
tidepool_tekpool_enable="NO"
```

Lastly, are there any tips or suggestions anyone could offer to improve/clean the code up a bit?  This sections seems to bother me a little. 

```
for profile in ${tidepool_profiles}; do
			echo "===> ${name} profile: ${profile}"
			/usr/local/etc/rc.d/${name} $1 ${profile}
			retcode="$?"
			if [ "0${retcode}" -ne 0 ]; then
				failed="${profile} (${retcode}) ${failed:-}"
			else
				success="${profile} ${success:-}"
			fi
		done
```

For example, should it be referencing the rc.d script directly or should I use the /usr/sbin/service command?


----------

