# Can't make something run at boot using rc.d



## Bink (Jul 15, 2014)

I'm trying to start a Node.js application at system boot, using PM2 (PM2 is something like Forever, it 's just a process manager for Node).  It's not starting automatically on bootup, but I _can_ start it manually if I run `# /usr/local/etc/rc.d/nodejs start`. Here is my rc.d script (/usr/local/etc/rc.d/nodejs):


```
#!/bin/sh

# PROVIDE: nodejs
# REQUIRE: NETWORKING
# KEYWORD: shutdown

name="nodejs"

. /etc/rc.subr

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

nodejs_start()
{
        /usr/node/node_modules/.bin/pm2 start /usr/node/app.js -i max --name my-node
}

load_rc_config $name
run_rc_command "$1"
```

The relevant lines in my rc.conf (/etc/rc.conf):

```
rc_debug="YES"
nodejs_enable="YES"
```

Here's the output from the log (/var/log/messages):

```
Jul 15 09:32:42 sugo-node root: /etc/rc: DEBUG: checkyesno: nodejs_enable is set to YES.
Jul 15 09:32:42 sugo-node root: /etc/rc: DEBUG: run_rc_command: doit: nodejs_start
```

I'm hoping there is just a simple oversight somewhere. I've even tried the fallback of running this as a cron job with the @reboot statement, but even that didn't work.


----------



## Bink (Jul 17, 2014)

My solution in the end, although a cop-out, was to use Nginx instead. I trialed it for other reasons and it did the job, as I'm only hosting some static files for a LAN based project. It's possibly better suited to that basic task than Node.js anyway. The Nginx port comes with functional rc.d scripts which was a welcome bonus.


----------



## ShelLuser (Jul 18, 2014)

I know it's a late response, and perhaps not of very much use to you, but I couldn't help noticing that I missed out a very important part in your test case up there...

Although you tested the script by manually running it you didn't test how the "rc.d structure" responded to it. So in your case above I would have expected you trying to run: `# service nodejs start` and then check what happened. Optionally also trying `# service nodejs status`.

Of course I can't be sure, but something tells me that the output of that command might have given you some hints to sort this out.


----------



## Johannes (Mar 13, 2015)

Another late response. I searched for a simple pm2 startup script some days ago and ended up creating one myself. If someone is interested: https://0x0a14.de/pm2-startup-script-for-freebsd/


----------

