admin管理员组

文章数量:1402331

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.

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.

Share Improve this question edited Apr 2 at 6:30 lcheylus 2,4911 gold badge19 silver badges34 bronze badges asked Mar 21 at 13:06 M.E.M.E. 5,5557 gold badges58 silver badges156 bronze badges 2
  • 1. First, SO is about programming, not OS features and settings, hence it seems somebody downvote your question 2. If you have to add a process from /usr/local/bin you have to use /usr/local/etc/rc.d instead of /etc/rc.d. which is reserved to systemland, not userland. 3. You speak about a jail but do not provide anything about this. 4. daemon if a utility that fork the process you provide and detach from terminal. 5. you have to run rcorder to see the order when you program starts and if it start with everything needed for its purpose (networking, ...). 6. Why not reading the handbook? – Valery S. Commented Mar 27 at 0:04
  • thanks for your comments. rc scripts (as in /etc/rc, /etc/rc.d/*, or /etc/rc.local) are shell scripts, so, writing an rc script is writing code, and thus it's a form of programming—even if it's usually more configuration-oriented than algorithmically interesting. – M.E. Commented Mar 29 at 23:13
Add a comment  | 

1 Answer 1

Reset to default 1

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.

本文标签: daemonImplementing a rc script to launch a custom serviceStack Overflow