2

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.

4

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"
0

On FreeBSD terminal I installed PM2:

$ sudo npm install pm2 -g

and then modified the rc.d script according to pm2:

#!/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

    ### Start server with PM2 ###
    /usr/local/bin/pm2 start /usr/home/ict/Documents/bluesky/server.js
    echo "$bluesky_msg"
}

run_rc_command "$1"

Now, the HTTP server is started as a daemon at boot time. However I really like the idea of FreeBSD daemon(8) suggested by @RichardSmith, I'm going to work on it.

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.