How do I run a node.js app as a background service

2018-12-31 10:02发布

Since this post has gotten a lot of attention over the years, I've listed the top solutions per platform at the bottom of this post.


Original post:

I want my node.js server to run in the background, i.e.: when I close my terminal I want my server to keep running. I've googled this and came up with this tutorial, however it doesn't work as intended. So instead of using that daemon script, I thought I just used the output redirection (the 2>&1 >> file part), but this too does not exit - I get a blank line in my terminal, like it's waiting for output/errors.

I've also tried to put the process in the background, but as soon as I close my terminal the process is killed as well.

So how can I leave it running when I shut down my local computer?


Top solutions:

25条回答
宁负流年不负卿
2楼-- · 2018-12-31 10:20

For people using newer versions of the daemon npm module - you need to pass file descriptors instead of strings:

var fs = require('fs');
var stdoutFd = fs.openSync('output.log', 'a');
var stderrFd = fs.openSync('errors.log', 'a');
require('daemon')({
    stdout: stdoutFd, 
    stderr: stderrFd
});
查看更多
孤独寂梦人
3楼-- · 2018-12-31 10:22

Check out fugue! Apart from launching many workers, you can demonize your node process too!

http://github.com/pgte/fugue

查看更多
谁念西风独自凉
4楼-- · 2018-12-31 10:24

To round out the various options suggested, here is one more: the daemon command in GNU/Linux, which you can read about here: http://libslack.org/daemon/manpages/daemon.1.html. (apologies if this is already mentioned in one of the comments above).

查看更多
旧人旧事旧时光
5楼-- · 2018-12-31 10:25

June 2017 Update:
Solution for Linux: (Red hat). Previous comments doesn't work for me. This works for me on Amazon Web Service - Red Hat 7. Hope this works for somebody out there.

A. Create the service file 
sudo vi /etc/systemd/system/myapp.service
[Unit]
Description=Your app
After=network.target

[Service]
ExecStart=/home/ec2-user/meantodos/start.sh
WorkingDirectory=/home/ec2-user/meantodos/

[Install]
WantedBy=multi-user.target

B. Create a shell file
/home/ec2-root/meantodos/start.sh
#!/bin/sh -
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 8080
npm start

then:
chmod +rx /home/ec2-root/meantodos/start.sh
(to make this file executable)

C. Execute the Following

sudo systemctl daemon-reload
sudo systemctl start myapp
sudo systemctl status myapp

(If there are no errors, execute below.  Autorun after server restarted.)
chkconfig myapp -add
查看更多
怪性笑人.
6楼-- · 2018-12-31 10:26

use nssm the best solution for windows, just download nssm, open cmd to nssm directory and type

nssm install <service name> <node path> <app.js path> 

eg: nssm install myservice "C:\Program Files\nodejs" "C:\myapp\app.js" 

this will install a new windows service which will be listed at services.msc from there you can start or stop the service, this service will auto start and you can configure to restart if it fails.

查看更多
笑指拈花
7楼-- · 2018-12-31 10:29

Try to run this command if you are using nohup -

nohup npm start 2>/dev/null 1>/dev/null&

You can also use forever to start server

forever start -c "npm start" ./ 
查看更多
登录 后发表回答