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.
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?
Problem courtesy of: Peter Kruithof
Copying my own answer from How do I 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.
myapp.service file (replacing 'myapp' with your app's name, obviously):
Copy your service file into the
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
Solution courtesy of: mikemaccana