Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get my Upstart script to run Node.js and Forever when server restarts

I've been setting up my server recently and today I had to restart it... then I realised all of my Node apps I had running weren't running anymore. I'm using Node Forever module to keep the apps running, but then I realised I still need to have them starting when my server restarts or shut downs and powers up again.

I have been researching the best way to do this, but what I'm trying just doesn't seem to work. I've created an Upstart script in my /etc/init/ folder on my Ubuntu Server 10.04LTS remote server and tried restarting and it doesn't seem to do anything. Nothing is getting listed when I run forever list.

Here is my current Upstart script I was trying out today:

#/etc/init/myapp.conf

start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown

script

    exec sudo /usr/local/bin/node /var/www/myapp/myapp.forever.js
end script

I use Forever in a Node script as I find it easier to configure it how I want. It's confirmed that the script runs just fine if I do this outside the script, there is just something wrong with the Upstart script itself. It seems to have the same permissions as all the other Upstart scripts in /etc/init/ folder.

As an additional note, I have gone through almost all the answers I could find here on StackOverflow, and that it how I got together the script that I have at present.

UPDATE:

With Tom's answer, I have now tried:

#/etc/init/myapp.conf

start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown

exec sudo /usr/local/bin/node /var/www/myapp/myapp.forever.js

But it's still not working.

So I don't know why this isn't running when I restart my server. Please help!

like image 346
littlejim84 Avatar asked Nov 04 '22 04:11

littlejim84


1 Answers

This is not a very happy setup. The way upstart works is that it starts your process running it using the process id for the start command. Forever JS works similarly, it is probably inspired by Upstart.

When you try to run forever.js with upstart, the forever process you create in your upstart script exits immediately after starting. Upstart counts on having the process continue to run.

When I tried to run forever using upstart, I wound up with five different forever process running because upstart thought it had failed to start forever, and it retried five times.

like image 183
Randy L Avatar answered Nov 11 '22 02:11

Randy L