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)
I use Supervisor for development. It just works. When ever you make changes to a .js file Supervisor automatically restarts your app with those changes loaded.
Here's a link to its Github page
Install :
You can easily make it watch other extensions with -e. Another command I use often is -i to ignore certain folders.
You can use nohup and supervisor to make your node app run in the background even after you log out.
If you are running nodejs in linux server, I think this is the best way.
Create a service script and copy to /etc/init/nodejs.conf
start service: sudo service nodejs start
stop service: sudo service nodejs stop
Sevice script
2016 Update: The node-windows/mac/linux series uses a common API across all operating systems, so it is absolutely a relevant solution. However; node-linux generates systemv init files. As systemd continues to grow in popularity, it is realistically a better option on Linux. PR's welcome if anyone wants to add systemd support to node-linux :-)
Original Thread:
This is a pretty old thread now, but node-windows provides another way to create background services on Windows. It is loosely based on the
nssm
concept of using anexe
wrapper around your node script. However; it useswinsw.exe
instead and provides a configurable node wrapper for more granular control over how the process starts/stops on failures. These processes are available like any other service:The module also bakes in some event logging:
Daemonizing your script is accomplished through code. For example:
The module supports things like capping restarts (so bad scripts don't hose your server) and growing time intervals between restarts.
Since node-windows services run like any other, it is possible to manage/monitor the service with whatever software you already use.
Finally, there are no
make
dependencies. In other words, a straightforwardnpm install -g node-windows
will work. You don't need Visual Studio, .NET, or node-gyp magic to install this. Also, it's MIT and BSD licensed.In full disclosure, I'm the author of this module. It was designed to relieve the exact pain the OP experienced, but with tighter integration into the functionality the Operating System already provides. I hope future viewers with this same question find it useful.
I am simply using the daemon npm module:
Lately I'm also using mon(1) from TJ Holowaychuk to start and manage simple node apps.
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.
Make a
myapp.service
file (replacing 'myapp' with your app's name, obviously):Note if you're new to Unix:
/var/www/myapp/app.js
should have#!/usr/bin/env node
on the very first line.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
This is taken from How we deploy node apps on Linux, 2018 edition, which also includes commands to generate an AWS/DigitalOcean/Azure CloudConfig to build Linux/node servers (including the
.service
file).If you simply want to run the script uninterrupted until it completes you can use
nohop
as already mentioned in the answers here. However, none of the answers provide a full command that also logsstdin
andstdout
.>>
means append toapp.log
.2>&1
makes sure that errors are also send tostdout
and added to theapp.log
.&
makes sure your current terminal is disconnected from command so you can continue working.If you want to run a node server (or something that should start back up when the server restarts) you should use systemd / systemctl.