Shell rc script doesn't run on boot

Hi,

I am playing around with node.js and want it to start at boot-time. I've made a primitive rc.d script, which works fine by using the typical commands, such as service miniserver start. But on system startup my app do not start.
I followed the handbook and looked at some of the base scripts. But I can't find the solution.

The rc script is saved in /usr/local/etc/rc.d/miniserver have the rights -rwxr-xr-x with owner root:wheel
In /etc/rc.conf I have added miniserver_enable="YES"

This is the scripts content:
Bash:
#!/bin/sh

# PROVIDE: miniserver
# REQUIRE: DAEMON NETWORKING FILESYSTEMS
# BEFORE: LOGIN
# KEYWORD:

. /etc/rc.subr

name="miniserver"

rcvar="miniserver_enable"

start_cmd=miniserver_start
stop_cmd=miniserver_stop
restart_cmd=miniserver_restart

pidfile="/var/run/miniserver.pid"

miniserver_start()
{
    NODE_ENV=production
    daemon -u www -t "Miniserver Daemon" -P "/var/run/miniserver.pid" /usr/local/bin/node /www/miniserver/start.js
    echo "Miniserver started"
}

miniserver_stop()
{
    miniserver_pid=`cat /var/run/miniserver.pid`
    kill $miniserver_pid
    echo "Miniserver stopped"
}

miniserver_restart()
{
    miniserver_stop
    miniserver_start
    echo "Miniserver restarted"
}

load_rc_config "miniserver"
run_rc_command "$1"
(I know, the stop and restart functions are weird, but on time I will make proper scripts to gracefully stop or restart the app)

What could be the reason it's prevented from beeing started at boot time?

Kind regards

Zabrah
 
Here is a very basic startup script that may help you:
 
Thank you for the link.
Anyhow after different changes I found out, rc is doing fine at startup. But Node.js crashes, if it should be started at boot time. I don't know, if this was the problem all the time, but for now, this actually problem is solved (or maybe never have been there).
If anyone with knowledge about Node.js is reading this, what do I have to do, to start it properly at boot-time? It seems not to crash by most things, but only anywhere in the code, when it comes to use http, maybe by start listening to a network-port. But I can't figure out, what's the specific problem, because Node does not generate useful output in exit-signal. Do I have to add additional things in REQUIRE in the rc script?
 
I would say try and run it without the daemon utility.
Also /etc/rc.local is good for testing a setup before moving it to a rc.d script.
 
I found out, without the daemon utility Node do not crash, but it blocks the boot process. I also tried rc.local but with same results.
I've seen pm2 before, but first want to try without it. I want to use it only if I can't figure out a way that's more native.
 
With /etc/rc.local you must use the ampersand ( & ) symbol at the end of the line to send the process to the background.
 
Thats strange. Even sent to background with & while booting cause Node to crash, while it works fine if done so manually.
I found out, that that crash only happens, if I use a specific function inside Node, using my own modules. Native modules are not affected.
In parallel I opened a thread in a Node.js community to get help also from them.

But one more question: Are processes in the background handled/restricted in a different way, especially at boot time?
 
Thats strange. Even sent to background with & while booting cause Node to crash, while it works fine if done so manually.
I found out, that that crash only happens, if I use a specific function inside Node, using my own modules. Native modules are not affected.
In parallel I opened a thread in a Node.js community to get help also from them.

But one more question: Are processes in the background handled/restricted in a different way, especially at boot time?

Two things to consider:
  • Environment variables are likely different. Log the output of env(1) to review.
  • Cron jobs will have no stdin to read from; is it expecting any input?
 
Back
Top