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:
- Systemd (Linux)
- Launchd (Mac)
- node-windows (Windows)
- PM2 (Node.js)
For people using newer versions of the daemon npm module - you need to pass file descriptors instead of strings:
Check out fugue! Apart from launching many workers, you can demonize your node process too!
http://github.com/pgte/fugue
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).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.
use nssm the best solution for windows, just download nssm, open cmd to nssm directory and type
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.
Try to run this command if you are using nohup -
You can also use forever to start server