简体   繁体   中英

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!

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.

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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM