Related
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)
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, PM2, etc are no longer necessary - your OS already handles these tasks.
Make a myapp.service file (replacing 'myapp' with your app's name, obviously):
[Unit]
Description=My app
[Service]
ExecStart=/var/www/myapp/app.js
Restart=always
User=nobody
# Note Debian/Ubuntu uses 'nogroup', RHEL/Fedora uses 'nobody'
Group=nogroup
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/myapp
[Install]
WantedBy=multi-user.target
Note if you're new to Unix: /var/www/myapp/app.js should have #!/usr/bin/env node on the very first line and have the executable mode turned on chmod +x app.js.
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).
You can use Forever, A simple CLI tool for ensuring that a given node script runs continuously (i.e. forever):
https://www.npmjs.org/package/forever
UPDATE - As mentioned in one of the answers below, PM2 has some really nice functionality missing from forever. Consider using it.
Original Answer
Use nohup:
nohup node server.js &
EDIT I wanted to add that the accepted answer is really the way to go. I'm using forever on instances that need to stay up. I like to do npm install -g forever so it's in the node path and then just do forever start server.js
This might not be the accepted way, but I do it with screen, especially while in development because I can bring it back up and fool with it if necessary.
screen
node myserver.js
>>CTRL-A then hit D
The screen will detach and survive you logging off. Then you can get it back back doing screen -r. Hit up the screen manual for more details. You can name the screens and whatnot if you like.
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 an exe wrapper around your node script. However; it uses winsw.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:
var Service = require('node-windows').Service;
// Create a new service object
var svc = new Service({
name:'Hello World',
description: 'The nodejs.org example web server.',
script: 'C:\\path\\to\\my\\node\\script.js'
});
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
svc.start();
});
// Listen for the "start" event and let us know when the
// process has actually started working.
svc.on('start',function(){
console.log(svc.name+' started!\nVisit http://127.0.0.1:3000 to see it in action.');
});
// Install the script as a service.
svc.install();
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 straightforward npm 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.
If you simply want to run the script uninterrupted until it completes you can use nohup as already mentioned in the answers here. However, none of the answers provide a full command that also logs stdin and stdout.
nohup node index.js >> app.log 2>&1 &
The >> means append to app.log.
2>&1 makes sure that errors are also send to stdout and added to the app.log.
The ending & 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.
UPDATE: i updated to include the latest from pm2:
for many use cases, using a systemd service is the simplest and most appropriate way to manage a node process. for those that are running numerous node processes or independently-running node microservices in a single environment, pm2 is a more full featured tool.
https://github.com/unitech/pm2
http://pm2.io
it has a really useful monitoring feature -> pretty 'gui' for command line monitoring of multiple processes with pm2 monit or process list with pm2 list
organized Log management -> pm2 logs
other stuff:
Behavior configuration
Source map support
PaaS Compatible
Watch & Reload
Module System
Max memory reload
Cluster Mode
Hot reload
Development workflow
Startup Scripts
Auto completion
Deployment workflow
Keymetrics monitoring
API
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" ./
PM2 also supports npm start
pm2 start npm -- start
If you are running OSX, then the easiest way to produce a true system process is to use launchd to launch it.
Build a plist like this, and put it into the /Library/LaunchDaemons with the name top-level-domain.your-domain.application.plist (you need to be root when placing it):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>top-level-domain.your-domain.application</string>
<key>WorkingDirectory</key>
<string>/your/preferred/workingdirectory</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/node</string>
<string>your-script-file</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
</dict>
</plist>
When done, issue this (as root):
launchctl load /Library/LaunchDaemons/top-level-domain.your-domain.application.plist
launchctl start top-level-domain.your-domain.application
and you are running.
And you will still be running after a restart.
For other options in the plist look at the man page here: https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man5/launchd.plist.5.html
I am simply using the daemon npm module:
var daemon = require('daemon');
daemon.daemonize({
stdout: './log.log'
, stderr: './log.error.log'
}
, './node.pid'
, function (err, pid) {
if (err) {
console.log('Error starting daemon: \n', err);
return process.exit(-1);
}
console.log('Daemonized successfully with pid: ' + pid);
// Your Application Code goes here
});
Lately I'm also using mon(1) from TJ Holowaychuk to start and manage simple node apps.
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 :
sudo npm install supervisor -g
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.
sudo nohup supervisor myapp.js &
Node.js as a background service in WINDOWS XP
Kudos goes to Hacksparrow at: http://www.hacksparrow.com/install-node-js-and-npm-on-windows.html for tutorial installing Node.js + npm for windows.
Kudos goes to Tatham Oddie at: http://blog.tatham.oddie.com.au/2011/03/16/node-js-on-windows/ for nnsm.exe implementation.
Installation:
Install WGET http://gnuwin32.sourceforge.net/packages/wget.htm via installer executable
Install GIT http://code.google.com/p/msysgit/downloads/list via installer executable
Install NSSM http://nssm.cc/download/?page=download via copying nnsm.exe into %windir%/system32 folder
Create c:\node\helloworld.js
// http://howtonode.org/hello-node
var http = require('http');
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
server.listen(8000);
console.log("Server running at http://127.0.0.1:8000/");
Open command console and type the following (setx only if Resource Kit is installed)
C:\node> set path=%PATH%;%CD%
C:\node> setx path "%PATH%"
C:\node> set NODE_PATH="C:\Program Files\nodejs\node_modules"
C:\node> git config --system http.sslcainfo /bin/curl-ca-bundle.crt
C:\node> git clone --recursive git://github.com/isaacs/npm.git
C:\node> cd npm
C:\node\npm> node cli.js install npm -gf
C:\node> cd ..
C:\node> nssm.exe install node-helloworld "C:\Program Files\nodejs\node.exe" c:\node\helloworld.js
C:\node> net start node-helloworld
A nifty batch goodie is to create c:\node\ServiceMe.cmd
#echo off
nssm.exe install node-%~n1 "C:\Program Files\nodejs\node.exe" %~s1
net start node-%~n1
pause
Service Management:
The services themselves are now accessible via Start-> Run->
services.msc or via Start->Run-> MSCONFIG-> Services (and check 'Hide
All Microsoft Services').
The script will prefix every node made via the batch script with
'node-'.
Likewise they can be found in the registry: "HKLM\SYSTEM\CurrentControlSet\Services\node-xxxx"
The accepted answer is probably the best production answer, but for a quick hack doing dev work, I found this:
nodejs scriptname.js & didn't work, because nodejs seemed to gobble up the &, and so the thing didn't let me keep using the terminal without scriptname.js dying.
But I put nodejs scriptname.js in a .sh file, and
nohup sh startscriptname.sh & worked.
Definitely not a production thing, but it solves the "I need to keep using my terminal and don't want to start 5 different terminals" problem.
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
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
description "DManager node.js server - Last Update: 2012-08-06"
author "Pedro Muniz - pedro.muniz#geeklab.com.br"
env USER="nodejs" #you have to create this user
env APPNAME="nodejs" #you can change the service name
env WORKDIR="/home/<project-home-dir>" #set your project home folder here
env COMMAND="/usr/bin/node <server name>" #app.js ?
# used to be: start on startup
# until we found some mounts weren't ready yet while booting:
start on started mountall
stop on shutdown
# Automatically Respawn:
respawn
respawn limit 99 5
pre-start script
sudo -u $USER echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/$APPNAME.log
end script
script
# Not sure why $HOME is needed, but we found that it is:
export HOME="<project-home-dir>" #set your project home folder here
export NODE_PATH="<project node_path>"
#log file, grant permission to nodejs user
exec start-stop-daemon --start --make-pidfile --pidfile /var/run/$APPNAME.pid --chuid $USER --chdir $WORKDIR --exec $COMMAND >> /var/log/$APPNAME.log 2>&1
end script
post-start script
# Optionally put a script here that will notifiy you node has (re)started
# /root/bin/hoptoad.sh "node.js has started!"
end script
pre-stop script
sudo -u $USER echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/$APPNAME.log
end script
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.
Use pm2 module. pm2 nodejs module
Since I'm missing this option in the list of provided answers I'd like to add an eligible option as of 2020: docker or any equivalent container platform. In addition to ensuring your application is working in a stable environment there are additional security benefits as well as improved portability.
There is docker support for Windows, macOS and most/major Linux distributions. Installing docker on a supported platform is rather straight-forward and well-documented. Setting up a Node.js application is as simple as putting it in a container and running that container while making sure its being restarted after shutdown.
Create Container Image
Assuming your application is available in /home/me/my-app on that server, create a text file Dockerfile in folder /home/me with content similar to this one:
FROM node:lts-alpine
COPY /my-app/ /app/
RUN cd /app && npm ci
CMD ["/app/server.js"]
It is creating an image for running LTS version of Node.js under Alpine Linux, copying the application's files into the image and runs npm ci to make sure dependencies are matching that runtime context.
Create another file .dockerignore in same folder with content
**/node_modules
This will prevent existing dependencies of your host system from being injected into container as they might not work there. The presented RUN command in Dockerfile is going to fix that.
Create the image using command like this:
docker build -t myapp-as-a-service /home/me
The -t option is selecting the "name" of built container image. This is used on running containers below.
Note: Last parameter is selecting folder containing that Dockerfile instead of the Dockerfile itself. You may pick a different one using option -f.
Start Container
Use this command for starting the container:
docker run -d --restart always -p 80:3000 myapp-as-a-service
This command is assuming your app is listening on port 3000 and you want it to be exposed on port 80 of your host.
This is a very limited example for sure, but it's a good starting point.
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).
Check out fugue! Apart from launching many workers, you can demonize your node process too!
http://github.com/pgte/fugue
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.
https://github.com/Unitech/pm2
I am surprised that nobody has mentioned Guvnor
I have tried forever, pm2, etc. But, when it comes to solid control and web based performance metrics, I have found Guvnor to be by far the best. Plus, it is also fully opensource.
Edit : However, I am not sure if it works on windows. I've only used it on linux.
has anyone noticed a trivial mistaken of the position of "2>&1" ?
2>&1 >> file
should be
>> file 2>&1
I use tmux for a multiple window/pane development environment on remote hosts. It's really simple to detach and keep the process running in the background. Have a look at tmux
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
});
If you are using pm2, you can use it with autorestart set to false:
$ pm2 ecosystem
This will generate a sample ecosystem.config.js:
module.exports = {
apps: [
{
script: './scripts/companies.js',
autorestart: false,
},
{
script: './scripts/domains.js',
autorestart: false,
},
{
script: './scripts/technologies.js',
autorestart: false,
},
],
}
$ pm2 start ecosystem.config.js
I received the following error when using #mikemaccana's accepted answer on a RHEL 8 AWS EC2 instance: (code=exited, status=216/GROUP)
It was due to using the user/group set to: 'nobody'.
Upon googling, it seems that using user/group as 'nobody'/'nogroup' is bad practice for daemons as answered here on the unix stack exchange.
It worked great after I set user/group to my actual user and group.
You can enter whomai and groups to see your available options to fix this.
My service file for a full stack node app with mongodb:
[Unit]
Description=myapp
After=mongod.service
[Service]
ExecStart=/home/myusername/apps/myapp/root/build/server/index.js
Restart=always
RestartSec=30
User=myusername
Group=myusername
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/home/myusername/apps/myapp
[Install]
WantedBy=multi-user.target
In case, for local development purpose, you need to start multiple instances of NodeJS app (express, fastify, etc.) then the concurrently might be an option. Here is a setup:
Prerequesites
Your NodeJS app (express, fastify, etc.) is placed at /opt/mca/www/mca-backend/app path.
Assuming that you are using node v.16 installed via brew install node#16
Setup
Install concurrently: npm install -g concurrently
Create a file ~/Library/LaunchAgents/mca.backend.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>mca.backend</string>
<key>LimitLoadToSessionType</key>
<array>
<string>Aqua</string>
<string>Background</string>
<string>LoginWindow</string>
<string>StandardIO</string>
<string>System</string>
</array>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/concurrently</string>
<string>--names</string>
<string>dev,prd</string>
<string>--success</string>
<string>all</string>
<string>--kill-others</string>
<string>--no-color</string>
<string>MCA_APP_STAGE=dev node ./server.mjs</string>
<string>MCA_APP_STAGE=prod node ./server.mjs</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/local/opt/node#16/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin</string>
</dict>
<key>WorkingDirectory</key>
<string>/opt/mca/www/mca-backend/app</string>
<key>StandardErrorPath</key>
<string>/opt/mca/www/mca-backend/err.log</string>
<key>StandardOutPath</key>
<string>/opt/mca/www/mca-backend/out.log</string>
</dict>
</plist>
Load and run: launchctl bootstrap gui/`id -u` $HOME/Library/LaunchAgents/mca.backend.plist
Get a status: launchctl print gui/`id -u`/mca.backend
Stop: launchctl kill SIGTERM gui/`id -u`/mca.backend
Start/Restart: launchctl kickstart -k -p gui/`id -u`/mca.backend
Unload if not needed anymore: launchctl bootout gui/`id -u`/mca.backend
IMPORTANT: Once you are loaded service with launchctl bootstrap any changes you made in file
~/Library/LaunchAgents/mca.backend.plist won't be in action until you unload the service (by using launchctl bootout)
and then load it again (by using launchctl bootstrap).
Troubleshooting
See launchd logs at: /private/var/log/com.apple.xpc.launchd/launchd.log
This answer is quite late to the party, but I found that the best solution was to write a shell script that used the both the screen -dmS and nohup commands.
screen -dmS newScreenName nohup node myserver.js >> logfile.log
I also add the >> logfile bit on the end so I can easily save the node console.log() statements.
Why did I use a shell script? Well I also added in an if statement that checked to see if the node myserver.js process was already running.
That way I was able to create a single command line option that both lets me keep the server going and also restart it when I have made changes, which is very helpful for development.
I have followed some posts and tutorials as well to create a script to start meteor project when server restart. i have followed answer mentioned in : How to run meteor on startup on Ubuntu server
Then I gave executable permission to script with "chmod +x meteor-server.sh".
I have tried to put this script in /etc/init.d and /etc/init folders but meteor project does not start at the reboot. I'm using ubuntu 16.04.
I would be grateful if you can show me the fault that i have done. Following code is my "meteor.server.sh" script file.
# meteorjs - meteorjs job file
description "MeteorJS"
author "Jc"
# When to start the service
start on runlevel [2345]
# When to stop the service
stop on runlevel [016]
# Automatically restart process if crashed
respawn
# Essentially lets upstart know the process will detach itself to the background
expect fork
# Run before process
pre-start script
cd /home/me/projects/cricket
echo ""
end script
# Start the process
exec meteor run -p 4000 --help -- production
First of all, there's already a very good tool mupx that allows you to deploy meteor projects to your own architecture, so there's really no need to do it by yourself unless you have a very good.
If you really need to deploy manually, this will take several steps. I am not going to cover all the details because you're specifically asking about the startup script and the remaining instruction should be easily accessible in the Internet.
1. Prepare your server
Install MongoDB unless you are planning to use a remote database.
Install NodeJS.
Install reverse proxy server, e.g. Nginx, or haproxy.
Install Meteor, which we will use as the build tool only.
2. Build your app
I am assuming here that you already have the source code of your app put on the server to which you're planning to deploy. Go to your project root and run the following command:
meteor build /path/to/your/build --directory
Please note that if /path/to/your/build exists it will be recursively deleted first, so be careful with that.
3. Install app dependencies
Go to /path/to/your/build/bundle/programs/server and run:
npm install
4. Prepare a run.sh script
The file can be of the following form:
export MONGO_URL="mongodb://127.0.0.1:27017/appName"
export ROOT_URL="http://myapp.example.com"
export PORT=3000
export METEOR_SETTINGS="{}"
/usr/bin/env node /path/to/your/build/bundle/main.js
I will assume you put it in /path/to/your/run.sh. A couple of notes here:
This form of MONGO_URL assumes you have MongoDB installed locally.
You will need to instruct your reverse proxy server to point your app trafic to port 3000.
METEOR_SETTINGS should be the output of JSON.stringify(settings) of whatever settings object you may have.
5. Prepare upstart script
With all the preparations we've made so far, the script can be as simple as
description "node.js server"
start on (net-device-up and local-filesystems and runlevel [2345])
stop on runlevel [016]
respawn
script
exec /path/to/your/run.sh
end script
This file should go to /etc/init/appName.conf.
Finally i got it to work. I have used following 2 scripts to run meteor on the startup.
First i put this service file(meteor.service) in /etc/systemd/system
[Unit]
Description = My Meteor Application
[Service]
ExecStart=/etc/init.d/meteor.sh
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=meteor
[Install]
WantedBy=multi-user.target
I have called a scipt using this service. I put this following script(meteor.sh) in /etc/init.d
#!/bin/sh -
description "Meteor Projects"
author "Janitha"
#start service on following run levels
start on runlevel [2345]
#stop service on following run levels
stop on runlevel [016]
#restart service if crashed
respawn
#set user/group to run as
setuid janitha
setgid janitha
chdir /home/janitha/projects/cricket_app
#export HOME (for meteor), change dir to plex requests dir, and run meteor
script
export HOME=/home/janitha
exec meteor
end script
I make both these file executable by using
chmod +x meteor.service
chmod +x meteor.sh
And i have used following two commands to enable the service
systemctl daemon-reload
systemctl enable meteor.service
I used this configurations successfully
In /etc/init.d add a file called meteor.sh
#!/bin/sh
export HOME="/home/user"
cd /home/user/meteor/sparql-fedquest
meteor --allow-superuser
You must give executions permissions to meteor.sh
sudo chmod 644 meteor.sh
Also you must create meteor.service in /etc/systemd/system
[Unit]
Description =Portal of bibliographic resources of University of Cuenca
Author = Freddy Sumba
[Service]
ExecStart=/etc/init.d/meteor.sh
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=meteor
[Install]
WantedBy=multi-user.target
Also you must give permissions to meteor.service
$ sudo chmod 644 meteor.service
Then, we need add the service to this start each time that the server reboot
$ systemctl enable meteor.service
And finally start the service
$ service meteor start
I have a simple meteor app that I'm running on an Amazon EC2 server. Everything is working great. I start it manually with my user via meteor in the project directory.
However, what I would like is for this app to
Run on boot
Be immune to hangups
I try running it via nohup meteor &, but when I try to log out of the EC2 instance, I get the "You have running jobs" message. Continuing to log out stops the app.
How can I get the app to start on startup and stay up (unless it crashes for some reason)?
Install forever and use a start script.
$ npm install -g forever
I have several scripts for managing my production environment - the start script looks something like:
#!/bin/bash
forever stopall
export MAIL_URL=...
export MONGO_URL=...
export MONGO_OPLOG_URL=...
export PORT=3000
export ROOT_URL=...
forever start /home/ubuntu/apps/myapp/bundle/main.js
exit 0
Conveniently, it will also append to a log file in ~/.forever which will show any errors encountered while running your app. You can get the location of the log file and other stats about your app with:
$ forever list
To get your app to start on startup, you'd need to do something appropriate for your flavor of linux. You can maybe just put the start script in /etc/rc.local. For ubuntu see this question.
Also note you really should be bundling your app if using it in production. See this comparison for more details on the differences.
I am using upstart on Ubuntu server which you should be able to easily install on Amazon linux.
This is roughly my /etc/init/myapp.conf:
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
respawn
respawn limit 99 5
script
export HOME="/home/deploy"
export NODE_ENV="production"
export MONGO_URL="mongodb://localhost:27017/myappdb"
export ROOT_URL=http://localhost
export MAIL_URL=smtp://localhost:25
export METEOR_SETTINGS='{"somesetting":true}'
cd /var/www/myapp/bundle/
exec sudo -u deploy PORT=3000 /usr/bin/node main.js >> /var/log/node.log 2>&1
end script
I can then manually start and stop myapp like this:
sudo start myapp
sudo stop myapp
I believe this package solves your problem: https://github.com/arunoda/meteor-up
which seems to use forever: https://github.com/nodejitsu/forever
I found an old PC and i want to use it as a dedicated Node.js test machine.
Basically i wanna write my apps on a win machine then copy them over samba to the node folder and launch them via ssh. Later, I would add an upstart script and copy it with samba to the server so that when i reboot the app starts automatically every time.
What do I need to install in order to properly run Node.js apps on my network on a dedicated Ubuntu server? Here is the list I came up with, please correct me if I'm wrong. Is there anything else?
ssh
samba (ftp or sftp should be the way to go but as it's a closed internal network and i have to access it from various os's samba is the simplest way to share files not considering security issues..most of the time i use a simple text editor)
"basic ubuntu server" files?
"LAMP" (?)
node.js
node package manager.
how do i install the latest Node.js, npm, and the init files on Ubuntu server. I saw that there was no simple sudo apt-get install nodejs npm.
What kind of script do I need to launch my apps and where do i put them (prefer native scripts)?
EDIT
After some testing i'm at a good point now, and here is what i did:
I installed ubuntu from a minimal CD
when it comes to choose the packages i selected ONLY ssh & samba
update the system
install the dependencies that u need to run node.js
install latest node from git
setup samba in my case i created the folder /var/nodejs for the scripts
put your testApp.js in the nodejs folder
start your testApp.js from ssh. *it won't work
3-update the system
sudo apt-get update && sudo apt-get upgrade
4-dependancies
sudo apt-get install g++ curl libssl-dev apache2-utils git-core make
5-install node
git clone git://github.com/ry/node.git
cd node
./configure
make
sudo make install
6-setup samba sudo nano /etc/samba/smb.conf
[nodejs]
comment = nodejs
workgroup = WG
security = USER
path = /var/nodejs
server string =Node JS
browsable = yes
read only = no
writeable = yes
create mask = 0777
7-testApp.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Node.js\n');
}).listen(80, "192.168.0.1");
console.log('Server running at http://192.168.0.1:80/');
8-Now everything should run...but:
You can run nodejs only as administrator appending "sudo" in front of the launch command
else as a normal user u don't have access to most of the ports under 1000.
A. How can i lauch my app on port 80 without using sudo?
And obviously if u launch you app with the command sudo node /var/nodejs/testApp.js
if u close the terminal the app will stop.
For that we use a init script.
After some reading i found that upstart is natively installed in ubuntu server and it's probably the best way to launch your apps.
B. I know u need to put the script into /etc/init/ with your appname and .conf extension.but how does that work?
what do i need to install to properly run node.js apps on my network on a dedicated ubuntu server?
You just need to install nodejs. nodejs can run on any port, so you don't need Apache or anything else.
how do i install the latest nodejs,npm,and the init files on ubuntu
server
Try to follow the steps outlined in this guide: http://howtonode.org/how-to-install-nodejs . Use the instructions for Ubuntu.
when i reboot the app starts automatically every time
One way to do this is to write a small script that will run on boot. The script would contain the instruction:
nodejs /path/to/app/app.js
Check out this SO answer on how to run a script on boot: https://stackoverflow.com/questions/3036/files-and-scripts-that-execute-on-boot
By your question, you sound about as lazy and impatient as I am, therefore use PPAs instead of building from the source. Just follow the node.js ubuntu directions.
In-fact I'm so lazy, I refuse to type in port numbers, hence I proxy all my node.js applications with nginx. (This is also the best way, and only way I can tell to have multiple servers "listening" on port 80). [Nginx's install guide.] Once you get nginx up, follow Chris Lea's guide.(http://wiki.nginx.org/Install) for the proxy.
BTW if you installed apache, make sure you purge it sudo apt-get purge apache*. This will most likely break your php apps, but that's why you're running node right? Just google how to run php with nignx.
Now for upstart & monit. Just follow this guide. NOTE: The guide has a typo so read the comments carefully.
As for samaba, you're on your own there.
TL;DR
Answer A: guide
Answer B: sudo cp my-node-app.conf /etc/init; sudo service my-node-app start
Edit 1
Upstart is Ubuntu's native utiltiy for starting background processes. Read all about it here.
#!upstart
description "node-app"
author "me"
env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
respawn
start on runlevel [23]
script
#set enviroment vars here
export NODE_ENV=production
#Uncommit if you need a pid file for monit
#echo $$ > /var/run/node-app.pid
exec /usr/bin/node /path/to/app.js 2>&1 >> /path/to/log/file/app.log
end script
#Logs start and stop time timestamps to the file
pre-start script
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /path/to/log/file/app.log
end script
pre-stop script
#Uncomment if you need a pid file for monit
#rm /var/run/yourprogram.pid
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /path/to/log/file/app.log
end script
Now start and stop your process by using service:
sudo service node-app start
Switch start with stop or status if needed.
If you are not using monit, just remove the pid lines. I really recommend using monit because you can configure it to give you email alert if your process dies or an error occurs in the log file.
What i would do:
install ubuntu
install apache and SVN, with svn repos accessible via http://
create a svn for each project
create a svn commit hook to autodeploy scripts to a folder
/svn//hooks/post-commit
REPOS="$1"
REV="$2"
cd /var/target/path
svn cleanup /var/target/path/
svn checkout -q --force file:///svn/ /var/target/path
svn cleanup /var/target/path/
exit 0
/svn//hooks/post_commit.sh
#!/bin/bash
REPOS="$1"
REV="$2"
# Files and directories can be distinguished, as directory paths are displayed with a trailing "/" character.
LOOK=/usr/bin/svnlook
SVN=/svn/
DEV=/var/target/path/
#mkdir /var/tmp/svn
cd /var/tmp/svn
for changes in `$LOOK changed $REPOS | awk '{print $1 "=" $2;}'`;
do
len=${#changes}
idx=`expr index "$changes" =`;
directory=${changes:$idx};
action=${changes:0:$idx-1};
if [ ${changes:len-1} = '/' ]
then
case "$action" in
"A" ) \
mkdir --mode=775 -p $DEV/$directory;
chown nobody:nobody $DEV/$directory;
chmod 775 $DEV/$directory;
;;
"D" ) \
rmdir $DEV/$directory;
;;
esac
else
case "$action" in
"A"|"U"|"UU" ) \
$SVN export --force --non-interactive -r HEAD -q file://$REPOS/$directory;
BASE=`basename $directory`;
DIR=`dirname $directory`;
chown nobody:nobody $BASE;
chmod 775 $BASE;
mkdir --mode=775 -p $DEV/$DIR;
cp -f --preserve=ownership $BASE $DEV/$DIR;
unlink $BASE;
;;
"D" ) \
rm -f $DEV/$directory;
;;
esac
fi
done
echo Updated dev subdomain
exit 1
install nodejs
install nodemon with npm install -g nodemon (out of my head - please check the manual)
create an upstart file for the node js script in /etc/init
description "nodejs with nodemonn"
author "etc"
start on startup
respawn
script
cd /var/target/project/dir/
exec nodemon /var/target/project/dir/main.js
end script
now all you have to do is work on the code and commit
when you commit the code is updated and the nodejs script is restarted
have fun coding !
I don't think samba is required, unless you want to develop on the server via samba. If you want to do that just skip the svn part and install samba, but setup nodemon upstart scripts - it will save you alot of hassle os ssh-ing.
I work this way because it allows me to write and test locally and then comit code to dev/test/prod servers quick and easy.
To solve the port 80 issue, just setup a firewall rule to redirect incoming tcp/80 to tcp/8080 (for instance) and listen in your nodejs script on that port.
More information here: https://serverfault.com/questions/112795/how-can-i-run-a-server-on-linux-on-port-80-as-a-normal-user
p.s. i did not add a complete step-by-step instruction for each item because there are plenty of guides out there that would a much better job than i can
on Ubuntu: startup booting Nodejs + socket.io by ubuntu user:
more /etc/init/noded.conf
# Ubuntu upstart file at /etc/init/noded.conf
description "noded.conf"
author "Nguyen Thanh Binh"
start on runlevel [2345]
stop on runlevel [06]
respawn
script
su - ubuntu -c "NODE_ENV=test exec sudo /usr/bin/node /home/ubuntu/server.js" >> /home/ubuntu/log.log &
end script
You can use forvere module to start a nodejs application in the background.
for full info see this http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever.
and I am just running an Ubuntu dedicated server with nodejs and it's working without a problem
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)
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, PM2, etc are no longer necessary - your OS already handles these tasks.
Make a myapp.service file (replacing 'myapp' with your app's name, obviously):
[Unit]
Description=My app
[Service]
ExecStart=/var/www/myapp/app.js
Restart=always
User=nobody
# Note Debian/Ubuntu uses 'nogroup', RHEL/Fedora uses 'nobody'
Group=nogroup
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/myapp
[Install]
WantedBy=multi-user.target
Note if you're new to Unix: /var/www/myapp/app.js should have #!/usr/bin/env node on the very first line and have the executable mode turned on chmod +x app.js.
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).
You can use Forever, A simple CLI tool for ensuring that a given node script runs continuously (i.e. forever):
https://www.npmjs.org/package/forever
UPDATE - As mentioned in one of the answers below, PM2 has some really nice functionality missing from forever. Consider using it.
Original Answer
Use nohup:
nohup node server.js &
EDIT I wanted to add that the accepted answer is really the way to go. I'm using forever on instances that need to stay up. I like to do npm install -g forever so it's in the node path and then just do forever start server.js
This might not be the accepted way, but I do it with screen, especially while in development because I can bring it back up and fool with it if necessary.
screen
node myserver.js
>>CTRL-A then hit D
The screen will detach and survive you logging off. Then you can get it back back doing screen -r. Hit up the screen manual for more details. You can name the screens and whatnot if you like.
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 an exe wrapper around your node script. However; it uses winsw.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:
var Service = require('node-windows').Service;
// Create a new service object
var svc = new Service({
name:'Hello World',
description: 'The nodejs.org example web server.',
script: 'C:\\path\\to\\my\\node\\script.js'
});
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
svc.start();
});
// Listen for the "start" event and let us know when the
// process has actually started working.
svc.on('start',function(){
console.log(svc.name+' started!\nVisit http://127.0.0.1:3000 to see it in action.');
});
// Install the script as a service.
svc.install();
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 straightforward npm 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.
If you simply want to run the script uninterrupted until it completes you can use nohup as already mentioned in the answers here. However, none of the answers provide a full command that also logs stdin and stdout.
nohup node index.js >> app.log 2>&1 &
The >> means append to app.log.
2>&1 makes sure that errors are also send to stdout and added to the app.log.
The ending & 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.
UPDATE: i updated to include the latest from pm2:
for many use cases, using a systemd service is the simplest and most appropriate way to manage a node process. for those that are running numerous node processes or independently-running node microservices in a single environment, pm2 is a more full featured tool.
https://github.com/unitech/pm2
http://pm2.io
it has a really useful monitoring feature -> pretty 'gui' for command line monitoring of multiple processes with pm2 monit or process list with pm2 list
organized Log management -> pm2 logs
other stuff:
Behavior configuration
Source map support
PaaS Compatible
Watch & Reload
Module System
Max memory reload
Cluster Mode
Hot reload
Development workflow
Startup Scripts
Auto completion
Deployment workflow
Keymetrics monitoring
API
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" ./
PM2 also supports npm start
pm2 start npm -- start
If you are running OSX, then the easiest way to produce a true system process is to use launchd to launch it.
Build a plist like this, and put it into the /Library/LaunchDaemons with the name top-level-domain.your-domain.application.plist (you need to be root when placing it):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>top-level-domain.your-domain.application</string>
<key>WorkingDirectory</key>
<string>/your/preferred/workingdirectory</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/node</string>
<string>your-script-file</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
</dict>
</plist>
When done, issue this (as root):
launchctl load /Library/LaunchDaemons/top-level-domain.your-domain.application.plist
launchctl start top-level-domain.your-domain.application
and you are running.
And you will still be running after a restart.
For other options in the plist look at the man page here: https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man5/launchd.plist.5.html
I am simply using the daemon npm module:
var daemon = require('daemon');
daemon.daemonize({
stdout: './log.log'
, stderr: './log.error.log'
}
, './node.pid'
, function (err, pid) {
if (err) {
console.log('Error starting daemon: \n', err);
return process.exit(-1);
}
console.log('Daemonized successfully with pid: ' + pid);
// Your Application Code goes here
});
Lately I'm also using mon(1) from TJ Holowaychuk to start and manage simple node apps.
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 :
sudo npm install supervisor -g
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.
sudo nohup supervisor myapp.js &
Node.js as a background service in WINDOWS XP
Kudos goes to Hacksparrow at: http://www.hacksparrow.com/install-node-js-and-npm-on-windows.html for tutorial installing Node.js + npm for windows.
Kudos goes to Tatham Oddie at: http://blog.tatham.oddie.com.au/2011/03/16/node-js-on-windows/ for nnsm.exe implementation.
Installation:
Install WGET http://gnuwin32.sourceforge.net/packages/wget.htm via installer executable
Install GIT http://code.google.com/p/msysgit/downloads/list via installer executable
Install NSSM http://nssm.cc/download/?page=download via copying nnsm.exe into %windir%/system32 folder
Create c:\node\helloworld.js
// http://howtonode.org/hello-node
var http = require('http');
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
server.listen(8000);
console.log("Server running at http://127.0.0.1:8000/");
Open command console and type the following (setx only if Resource Kit is installed)
C:\node> set path=%PATH%;%CD%
C:\node> setx path "%PATH%"
C:\node> set NODE_PATH="C:\Program Files\nodejs\node_modules"
C:\node> git config --system http.sslcainfo /bin/curl-ca-bundle.crt
C:\node> git clone --recursive git://github.com/isaacs/npm.git
C:\node> cd npm
C:\node\npm> node cli.js install npm -gf
C:\node> cd ..
C:\node> nssm.exe install node-helloworld "C:\Program Files\nodejs\node.exe" c:\node\helloworld.js
C:\node> net start node-helloworld
A nifty batch goodie is to create c:\node\ServiceMe.cmd
#echo off
nssm.exe install node-%~n1 "C:\Program Files\nodejs\node.exe" %~s1
net start node-%~n1
pause
Service Management:
The services themselves are now accessible via Start-> Run->
services.msc or via Start->Run-> MSCONFIG-> Services (and check 'Hide
All Microsoft Services').
The script will prefix every node made via the batch script with
'node-'.
Likewise they can be found in the registry: "HKLM\SYSTEM\CurrentControlSet\Services\node-xxxx"
The accepted answer is probably the best production answer, but for a quick hack doing dev work, I found this:
nodejs scriptname.js & didn't work, because nodejs seemed to gobble up the &, and so the thing didn't let me keep using the terminal without scriptname.js dying.
But I put nodejs scriptname.js in a .sh file, and
nohup sh startscriptname.sh & worked.
Definitely not a production thing, but it solves the "I need to keep using my terminal and don't want to start 5 different terminals" problem.
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
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
description "DManager node.js server - Last Update: 2012-08-06"
author "Pedro Muniz - pedro.muniz#geeklab.com.br"
env USER="nodejs" #you have to create this user
env APPNAME="nodejs" #you can change the service name
env WORKDIR="/home/<project-home-dir>" #set your project home folder here
env COMMAND="/usr/bin/node <server name>" #app.js ?
# used to be: start on startup
# until we found some mounts weren't ready yet while booting:
start on started mountall
stop on shutdown
# Automatically Respawn:
respawn
respawn limit 99 5
pre-start script
sudo -u $USER echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/$APPNAME.log
end script
script
# Not sure why $HOME is needed, but we found that it is:
export HOME="<project-home-dir>" #set your project home folder here
export NODE_PATH="<project node_path>"
#log file, grant permission to nodejs user
exec start-stop-daemon --start --make-pidfile --pidfile /var/run/$APPNAME.pid --chuid $USER --chdir $WORKDIR --exec $COMMAND >> /var/log/$APPNAME.log 2>&1
end script
post-start script
# Optionally put a script here that will notifiy you node has (re)started
# /root/bin/hoptoad.sh "node.js has started!"
end script
pre-stop script
sudo -u $USER echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/$APPNAME.log
end script
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.
Use pm2 module. pm2 nodejs module
Since I'm missing this option in the list of provided answers I'd like to add an eligible option as of 2020: docker or any equivalent container platform. In addition to ensuring your application is working in a stable environment there are additional security benefits as well as improved portability.
There is docker support for Windows, macOS and most/major Linux distributions. Installing docker on a supported platform is rather straight-forward and well-documented. Setting up a Node.js application is as simple as putting it in a container and running that container while making sure its being restarted after shutdown.
Create Container Image
Assuming your application is available in /home/me/my-app on that server, create a text file Dockerfile in folder /home/me with content similar to this one:
FROM node:lts-alpine
COPY /my-app/ /app/
RUN cd /app && npm ci
CMD ["/app/server.js"]
It is creating an image for running LTS version of Node.js under Alpine Linux, copying the application's files into the image and runs npm ci to make sure dependencies are matching that runtime context.
Create another file .dockerignore in same folder with content
**/node_modules
This will prevent existing dependencies of your host system from being injected into container as they might not work there. The presented RUN command in Dockerfile is going to fix that.
Create the image using command like this:
docker build -t myapp-as-a-service /home/me
The -t option is selecting the "name" of built container image. This is used on running containers below.
Note: Last parameter is selecting folder containing that Dockerfile instead of the Dockerfile itself. You may pick a different one using option -f.
Start Container
Use this command for starting the container:
docker run -d --restart always -p 80:3000 myapp-as-a-service
This command is assuming your app is listening on port 3000 and you want it to be exposed on port 80 of your host.
This is a very limited example for sure, but it's a good starting point.
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).
Check out fugue! Apart from launching many workers, you can demonize your node process too!
http://github.com/pgte/fugue
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.
https://github.com/Unitech/pm2
I am surprised that nobody has mentioned Guvnor
I have tried forever, pm2, etc. But, when it comes to solid control and web based performance metrics, I have found Guvnor to be by far the best. Plus, it is also fully opensource.
Edit : However, I am not sure if it works on windows. I've only used it on linux.
has anyone noticed a trivial mistaken of the position of "2>&1" ?
2>&1 >> file
should be
>> file 2>&1
I use tmux for a multiple window/pane development environment on remote hosts. It's really simple to detach and keep the process running in the background. Have a look at tmux
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
});
If you are using pm2, you can use it with autorestart set to false:
$ pm2 ecosystem
This will generate a sample ecosystem.config.js:
module.exports = {
apps: [
{
script: './scripts/companies.js',
autorestart: false,
},
{
script: './scripts/domains.js',
autorestart: false,
},
{
script: './scripts/technologies.js',
autorestart: false,
},
],
}
$ pm2 start ecosystem.config.js
I received the following error when using #mikemaccana's accepted answer on a RHEL 8 AWS EC2 instance: (code=exited, status=216/GROUP)
It was due to using the user/group set to: 'nobody'.
Upon googling, it seems that using user/group as 'nobody'/'nogroup' is bad practice for daemons as answered here on the unix stack exchange.
It worked great after I set user/group to my actual user and group.
You can enter whomai and groups to see your available options to fix this.
My service file for a full stack node app with mongodb:
[Unit]
Description=myapp
After=mongod.service
[Service]
ExecStart=/home/myusername/apps/myapp/root/build/server/index.js
Restart=always
RestartSec=30
User=myusername
Group=myusername
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/home/myusername/apps/myapp
[Install]
WantedBy=multi-user.target
In case, for local development purpose, you need to start multiple instances of NodeJS app (express, fastify, etc.) then the concurrently might be an option. Here is a setup:
Prerequesites
Your NodeJS app (express, fastify, etc.) is placed at /opt/mca/www/mca-backend/app path.
Assuming that you are using node v.16 installed via brew install node#16
Setup
Install concurrently: npm install -g concurrently
Create a file ~/Library/LaunchAgents/mca.backend.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>mca.backend</string>
<key>LimitLoadToSessionType</key>
<array>
<string>Aqua</string>
<string>Background</string>
<string>LoginWindow</string>
<string>StandardIO</string>
<string>System</string>
</array>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/concurrently</string>
<string>--names</string>
<string>dev,prd</string>
<string>--success</string>
<string>all</string>
<string>--kill-others</string>
<string>--no-color</string>
<string>MCA_APP_STAGE=dev node ./server.mjs</string>
<string>MCA_APP_STAGE=prod node ./server.mjs</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/local/opt/node#16/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin</string>
</dict>
<key>WorkingDirectory</key>
<string>/opt/mca/www/mca-backend/app</string>
<key>StandardErrorPath</key>
<string>/opt/mca/www/mca-backend/err.log</string>
<key>StandardOutPath</key>
<string>/opt/mca/www/mca-backend/out.log</string>
</dict>
</plist>
Load and run: launchctl bootstrap gui/`id -u` $HOME/Library/LaunchAgents/mca.backend.plist
Get a status: launchctl print gui/`id -u`/mca.backend
Stop: launchctl kill SIGTERM gui/`id -u`/mca.backend
Start/Restart: launchctl kickstart -k -p gui/`id -u`/mca.backend
Unload if not needed anymore: launchctl bootout gui/`id -u`/mca.backend
IMPORTANT: Once you are loaded service with launchctl bootstrap any changes you made in file
~/Library/LaunchAgents/mca.backend.plist won't be in action until you unload the service (by using launchctl bootout)
and then load it again (by using launchctl bootstrap).
Troubleshooting
See launchd logs at: /private/var/log/com.apple.xpc.launchd/launchd.log
This answer is quite late to the party, but I found that the best solution was to write a shell script that used the both the screen -dmS and nohup commands.
screen -dmS newScreenName nohup node myserver.js >> logfile.log
I also add the >> logfile bit on the end so I can easily save the node console.log() statements.
Why did I use a shell script? Well I also added in an if statement that checked to see if the node myserver.js process was already running.
That way I was able to create a single command line option that both lets me keep the server going and also restart it when I have made changes, which is very helpful for development.