daemonfreebsdinitjailrc.d

Implementing a rc script to launch a custom service


I have a proprietary service inside a FreeBSD 14 jail that I launch as follows:

/opt/bin/rserver_tcp >> /var/log/rserver_tcp.log 2>&1

This is a blocking service which uses stdout and stderr. So it does not return to command line unless you press CONTROL+C or kill the process.

I am trying to write a custom rc script to launch this.

The script also requires /usr/local/bin in the PATH. So I use env in the /etc/rc.d script accordingly. This is what I have tried so far:

/etc/rc.d/rserver_tcp

#!/bin/sh
# PROVIDE: rserver_tcp
# REQUIRE: DAEMON
# KEYWORD: shutdown

. /etc/rc.subr

name="rserver_tcp"
rcvar="rserver_tcp_enable"
desc="rserver_tcp service"
pidfile="/var/run/rserver_tcp.pid"
start_cmd="rserver_tcp_start"
stop_cmd="rserver_tcp_stop"

rserver_tcp_start() {
    echo "Starting rserver_tcp..."
    # Start the daemon in the background and redirect output properly.
    daemon -p "${pidfile}" env PATH="$PATH:/usr/local/bin" /opt/bin/rserver_tcp >> /var/log/rserver_tcp.log 2>&1
}

rserver_tcp_stop() {
    echo "Stopping rserver_tcp..."
    if [ -f "${pidfile}" ]; then
        kill "$(cat ${pidfile})" && rm -f "${pidfile}"
    else
        echo "PID file not found; is rserver_tcp running?"
    fi
}

load_rc_config "${name}"
: ${rserver_tcp_enable:=no}

run_rc_command "$1"

It works perfectly when I start/stop manually:

# service rserver_tcp onestart
Starting rserver_tcp...
# service rserver_tcp onestop
Stopping rserver_tcp...

However, when I do this in /etc/rc.conf:

rserver_tcp_start="yes"

then when I try start the jail from the server that contains the jail it keeps forever starting and it never returns to shell.

It works normally if I remove that line from /etc/rc.conf.


Solution

  • the service uses networking, hence the rc script must reflect this depedency.

    Modifying the following line to the rc.d script fixed the issue:

    # REQUIRE: networking
    

    Manually launching the script was working fine since networking was already in place.