5

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!

littlejim84
  • 9,071
  • 15
  • 54
  • 77
  • Why do you use Forever at all? I run node executable directly via Upstart and it works and restarts just fine. – Milan Babuškov Jun 25 '12 at 21:08
  • @hiteshjoshi Why? It wasn't the right answer I'm afraid. – littlejim84 Sep 24 '12 at 06:35
  • I have same problem.. am using this reference https://www.exratione.com/2013/02/nodejs-and-forever-as-a-service-simple-upstart-and-init-scripts-for-ubuntu/ – Michael Dausmann Nov 22 '13 at 10:23
  • My problem turned out to be vagrant related so I fixed with this ref http://razius.com/2013/11/launching-services-after-vagrant-mount/ You didn't specify so I suppose its a physical server you are playing with so this isn't really relevant. suggest you try the method mentioned in my comment above i.e. using forever command line – Michael Dausmann Nov 22 '13 at 21:09

3 Answers3

2

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.

Randy L
  • 14,384
  • 14
  • 44
  • 73
0

I've opted to use an @reboot statement in the user's crontab file, which will execute forever on server restarts.

@reboot forever start app.js

Additional Reading - http://www.cyberciti.biz/faq/linux-execute-cron-job-after-system-reboot/

0

Did you try doing it without the start script lines?

description "my server"
author  "name"

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

#respawn if you were not using forever

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

Source: http://caolanmcmahon.com/posts/deploying_node_js_with_upstart

Tom
  • 8,536
  • 31
  • 133
  • 232
  • Hello @Tom. I've tried what you suggested and it is still not working. I am reading through your useful link now... Man, it's driving me mad! – littlejim84 Mar 10 '12 at 13:48