FreeBSD rc.d script doesn’t work when starting up

freebsdrc.dshshell-scripting

I am trying to write a rc.d script to startup the fastcgi-mono-server4 on FreeBSD when the computer starts up – in order to run it with nginx.

The script works when I execute it while being logged in on the server – but when booting I get the following message:

eval: -applications=192.168.50.133:/:/usr/local/www/nginx: not found

The script looks as follows:

#!/bin/sh

# PROVIDE: monofcgid
# REQUIRE: LOGIN nginx
# KEYWORD: shutdown


. /etc/rc.subr

name="monofcgid"
rcvar="monofcgid_enable"
stop_cmd="${name}_stop"
start_cmd="${name}_start"
start_precmd="${name}_prestart"
start_postcmd="${name}_poststart"
stop_postcmd="${name}_poststop"
command=$(which fastcgi-mono-server4)
apps="192.168.50.133:/:/usr/local/www/nginx"
pidfile="/var/run/${name}.pid"

monofcgid_prestart()
{
        if [ -f $pidfile ]; then
                echo "monofcgid is already running."
                exit 0
        fi
}

monofcgid_start()
{
        echo "Starting monofcgid."
        ${command} -applications=${apps} -socket=tcp:127.0.0.1:9000 &
}


monofcgid_poststart()
{
        MONOSERVER_PID=$(ps ax | grep mono/4.0/fastcgi-m | grep -v grep | awk '{print $1}')
        if [ -f $pidfile ]; then
                rm $pidfile
        fi
        if [ -n $MONOSERVER_PID ]; then
                echo $MONOSERVER_PID > $pidfile
        fi
}

monofcgid_stop()
{
        if [ -f $pidfile ]; then
                echo "Stopping monofcgid."
                kill $(cat $pidfile)
                echo "Stopped monofcgid."
        else
                echo "monofcgid is not running."
                exit 0
        fi
}

monofcgid_poststop()
{
        rm $pidfile
}


load_rc_config $name
run_rc_command "$1"

In case it is not already super clear, I am fairly new to both FreeBSD and sh scripts, so I'm kind of prepared for some obvious little detail I overlooked.

I would very much like to know exactly why this is failing and how to solve it, but also if anyone has a better way of accomplishing this, then I am all open to ideas.

Best Answer

The problem you've stated is possibly related to difference in PATH of you when you execute script as logged-in user and when script is executed at startup.

The output of 'which' depends on PATH. So if place where your executable resides is not on PATH, it will return nothing.

I'd advice you to specify path to executable in $command explicitly. Or modify PATH on top of this script like this:

PATH="${PATH}:/path/to/where/daemon/lies"
Related Topic