Xi 张熹
Xi 张熹

Reputation: 11071

Why does Forever for Node.JS not work in rc.local?

I am deploying a node.js app to a Ubuntu server on EC2, and I am using Forever to keep it running ... forever.

The problem is, if I type in following in a console:

forever start ~/path/myapp.js

It works just fine. But I want the app automatically starts when on startup, so I appended the line to /etc/rc.local, and it stopped working. Everything else in rc.local still works.

I am not familiar with Linux so my best guess is some important component hasn't initialized when running rc.local. Is there somewhere else I can append the line?

Thanks,

Upvotes: 0

Views: 1606

Answers (2)

Ada Richards
Ada Richards

Reputation: 313

This is a very late reply, but I just ran into this. For me, using forever list says that nothing is running, but doing sudo forever list will show that the server is in fact running.

This is because rc.local is run as root, so your forever command is run as root, and only root can see it in the list.

I don't know the best practices, but I got around it by calling my forever start script from rc.local like this, where "pi" is the user I want to launch the forever process:

su pi -c './start'

If it helps, this is my abridged ./start script, yours may differ, or you can possibly put its contents directly in rc.local:

#!/bin/sh forever start --append -l forever.log -o server.log -e error.log server.js

Upvotes: 0

geoffreak
geoffreak

Reputation: 2328

I'm pretty sure it has something to do with the user who is actually running the commands in /etc/rc.local. As ~ specifies the user's home directory, this may be non-existent, different than expected, or just not loaded when the script runs. I would suggest trying the following:

forever start /path/to/home/path/myapp.js

Upvotes: 1

Related Questions