FreeBSD rc.d script doesn’t start as a daemon

freebsdrc.dshell-scripting

I have developed the following script at location /usr/local/etc/rc.d/bluesky

#!/bin/sh

# PROVIDE: bluesky
# REQUIRE: mysql sshd
# BEFORE:  
# KEYWORD: 

. /etc/rc.subr

name="bluesky"
rcvar=bluesky_enable

start_cmd="${name}_start"
stop_cmd=":"

load_rc_config $name
: ${bluesky_enable:=no}
: ${bluesky_msg="HTTP server starts ..."}

bluesky_start(){
    echo $PATH
    export PATH=$PATH:/usr/local/bin/
    echo $PATH

    ### Run Node server ###
    /usr/local/bin/node /usr/home/ict/Documents/bluesky/server.js
    echo "$bluesky_msg"
}

run_rc_command "$1"

I also have enabled it on my /etc/rc.conf file:

bluesky_enable="YES"

When I reboot the server, the script works fine and starts the HTTP server at port 80. The only problem is that the script won't be sent to background or won't be started as a daemon. I wonder how I can run script at boot time in the background or as a daemon.

Best Answer

The RC script itself is not intended to daemonize, but is expected to start and stop the daemon.

If your service does not have an option to start as a daemon, you can use daemon(8) to manage that part.

An example:

#!/bin/sh

# PROVIDE: ...
# REQUIRE: ...

. /etc/rc.subr

name="..."

rcvar=${name}_enable
pidfile="/var/run/${name}.pid"
command="/usr/sbin/daemon"
command_args="-c -f -P ${pidfile} -r /usr/local/libexec/${name}"
load_rc_config $name
run_rc_command "$1"