1

I'm trying to configure multiple Zope-Instances as daemons in FreeBSD. Each instance gets a start script in /usr/local/etc/rc.d. Starting works fine, but invoking status or stop is problematic because the PIDs of the running instances get confused (although the PIDs are different, the startscript cannot tell them apart).

Here is the template of the rc scripts:

instancename="%%instancename%%"
name="$instancename"

rcvar="${name}_enable"

zope="/usr/local/opt/zope"
python="${zope}/bin/python"
command_interpreter="$python"
command="${zope}/bin/runwsgi -v /usr/local/www/zope-instances/${instancename}/etc/zope.ini -d"

start_cmd="/usr/sbin/daemon -u myuser $command"

load_rc_config "$name"
run_rc_command $*

The values between "%%" are set differently for each instance.

When I try to get the status or to stop the process (service instancename status or service instancename stop) the PID of the last instance started is used. With processes able to create a pid-file this is not the case, but runwsgi, the script I need, doesn't create a pid-file.

I understand that procname, which defaults to the command variable, is used to tell the processes apart, but I don't know how to set it properly for my needs.

1

I think you are on the right path but that you expect the rc framework to handle more things automatically than it actually does.

It looks like you might be familiar with Practical rc.d scripting in BSD as you touch upon:

For instance, stop must know the PID of the process to terminate it. In the present case, rc.subr(8) will scan through the list of all processes, looking for a process with its name equal to $procname. The latter is another variable of meaning to rc.subr(8), and its value defaults to that of command. In other words, when we set command, procname is effectively set to the same value.

Your life will become easier if you accept that you do not have a "simple" daemon and look through the next section with the "advanced" daemon. So rather than setting procname to the correct name so it can scan for the PID - simply set the PID file. pidfile is a known entity which rc.subr(8) understands.

You are using daemon to detach from the terminal and that handles pid files nicely.

So if you add:

pidfile="/var/run/${name}.pid"

And change your start_cmd:

start_cmd="/usr/sbin/daemon -P ${pidfile} -u myuser $command"

Then you should be good to go.

Another nice article outlining a simple rc script is Supervised FreeBSD rc.d script for a Go daemon - the gist of it is as simple as:

#!/bin/sh
#
# PROVIDE: goprogram
# REQUIRE: networking
# KEYWORD:

. /etc/rc.subr

name="goprogram"
rcvar="goprogram_enable"
goprogram_user="goprogram"
goprogram_command="/usr/local/goprogram/goprogram"
pidfile="/var/run/goprogram/${name}.pid"
command="/usr/sbin/daemon"
command_args="-P ${pidfile} -r -f ${goprogram_command}"

load_rc_config $name
: ${goprogram_enable:=no}

run_rc_command "$1"

Notice how the main difference is that they control the pid file rather than relying on $procname

1
  • Thank you! In the meantime I had figured it out, having found exactly the web pages you mention. I should have read the man-page about daemon before asking the question... Feb 27 '19 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.