Peter Kruithof Peter Kruithof - 5 months ago 12
Node.js Question

Node.js as a background service

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:


Copying my own answer from How to run a Node.js application as its own process?

2015 answer: nearly every Linux distro comes with systemd, which means forever, monit, etc are no longer necessary - your OS already handles these tasks.

Make a myapp.service file (replacing 'myapp' with your app's name, obviously):

Description=My app



Copy your service file into the /etc/systemd/system.

Start it with systemctl start myapp.

Enable it to run on boot with systemctl enable myapp.

See logs with journalctl -u myapp

More details at: How we deploy node apps on Linux, 2016 edition