开发者

How does one start a node.js server as a daemon process?

开发者 https://www.devze.com 2023-02-08 21:05 出处:网络
In Python Twisted, you have the twistd command that helps you with a number of things related to running your application (daemonize it for example).

In Python Twisted, you have the twistd command that helps you with a number of things related to running your application (daemonize it for example).

How do you daemo开发者_运维技巧nize a node.js server so that it can run even after the current session is closed?


Forever is answer to your question.

Install

$ curl https://npmjs.org/install.sh | sh
$ npm install forever
# Or to install as a terminal command everywhere:
$ npm install -g forever

Usage

Using Forever from the command line

$ forever start server.js

Using an instance of Forever from Node.js

var forever = require('forever');

  var child = new forever.Forever('your-filename.js', {
    max: 3,
    silent: true,
    args: []
  });

  child.on('exit', this.callback);
  child.start();


If you need your process to daemonize itself, not relaying on forever - you can use the daemonize module.

$ npm install daemonize2

Then just write your server file as in example:

var daemon = require("daemonize2").setup({
    main: "app.js",
    name: "sampleapp",
    pidfile: "sampleapp.pid"
});

switch (process.argv[2]) {

    case "start":
        daemon.start();
        break;

    case "stop":
        daemon.stop();
        break;

    default:
        console.log("Usage: [start|stop]");
}

Mind you, that's rather a low level approach.


To start a systemd service manager daemon, write a service file. For example, create a file /etc/systemd/system/myservice.service.

[Unit]
Description=myservice-description
After=network.target

[Service]
ExecStart=/opt/myservice-location/src/node/server.js --args=here
Restart=always
User=me
Group=group
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/opt/myservice-location

[Install]
WantedBy=multi-user.target

Be sure to shebang your server.js (or the whatever the main file is that you execute). Or else consider adding the node executable that you want to use by providing the full, absolute path in the ExecStart= attribute of the service file.

#!/usr/bin/env node
// here is the content of the file "server.js"
...

Remember to update the service manager daemon after every change to the myservice.service file.

$ systemctl daemon-reload

Then start the service running and enable the service to start at boot.

$ systemctl start myservice
$ systemctl enable myservice


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


The simplest approach would just to send the command to the background.

$ node server.js &

Then you can kill the process at a later time. I usually do the following:

$ killall node

Note: I'm running OS X.


You can try:

$ nohup node server.js &

It work for me on Mac and Linux.

The output will be in the ./nohup.out file

But I still recommend you use pm2 or forever, because they are easily used for restarting, stopping and logging.


There are more advanced general-purpose runners, such as monit and runit.


For the background on the normal way to daemonise on a POSIX system you can search for the C method.

I have not seen enough methods in the node.js API to allow it to be done the C way by hand. However, when using child_process, you can have node.js do it for you:

http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

I consider this a potential waste of time because there's a good chance your system provides the same.

For example:

http://libslack.org/daemon/manpages/daemon.1.html

If you want something portable (cross platform) the other posts offer solutions that might suffice.


2022 - PM2

There is a better and one of the most popular solutions for that and it's called pm2 (npm package link).

To run one or multiple Node.js servers you need to install it: npm install pm2 -g

To run: pm2 start app.js

To stop: pm2 stop nameOfAppFromList


You can also list your running apps pm2 list

To check logs of a specific one, run pm2 logs nameOfAppFromList

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号