Nodejs authoring daemon

Nodejs is currently very simple to write a daemon, and there are handy apis available in 6.3.1 that make it easier to create a daemon. This article describes how daemons are created, not the tasks that daemons perform.

How to start the daemon process

How do we create daemons if we are not in a Nodejs environment? The process is as follows:

  1. Create process A.
  2. To create process B in process A, we can use fork, or other methods.
  3. Execute on process BsetsidMethods.
  4. Process A exits, and process B is taken over by init. In this case, process B is the daemon.

Setsid,

Setsid does three things:

  1. The process becomes the session leader for a new session.
  2. The process becomes the leader of a new process group.
  3. The process does not control the terminal.

However, there is no direct encapsulation of the setsid method in Nodejs, and browsing through the documentation shows that there is a place to call it.

Start the child process method in Nodejs

To create a child process, use spawn in clild_process as follows:

var spawn = require('child_process').spawn;
var process = require('process');

var p = spawn('node',['b.js']);
console.log(process.pid, p.pid);
Copy the code

Note that only the PID of the current process and the PID of the child process are printed here, and I did not exit the parent process for the sake of observation.

B. Js code is very simple, open a resource, do not stop writing data.

var fs = require('fs'); var process = require('process'); fs.open("/Users/mebius/Desktop/log.txt",'w',function(err, fd){ console.log(fd); while(true) { fs.write(fd,process.pid+"\n",function(){}); }});Copy the code

After running, the effect is shown as follows:

Let’s look at the process under the top command.

The parent process PID is 17055, the child process PPID is 17055, and its PID is 17056.

Call to setsid in Nodejs

At this point, the daemon is halfway done, so call the setsid method and exit the parent process.

The code is modified as follows:

var spawn = require('child_process').spawn;
var process = require('process');

var p = spawn('node',['b.js'],{
        detached : true
    });
console.log(process.pid, p.pid);
process.exit(0);
Copy the code

In the spawn third argument, you can set the detached property, which, if true, calls the setsid method. This satisfies our daemon requirements.

Run the command here.

View the top command

As you can see, there is currently only one process with a PID of 17062. This process is the daemon we want.

Since the PID is different each time you run it, the child process has a different PID than the first time.

conclusion

The most important thing about daemons is stability. If the daemon dies, all its children will be orphaned and taken over by the init process, which we don’t want. Meanwhile, daemons have a lot of scope for managing child processes. For example, in PM2, a process can be started 4 times at the same time for multiple CPU cores (it is possible that your process is running in the same core), a process can be restarted automatically when it dies, etc. These things are waiting for us to build wheels.

In general, Nodejs is a relatively simple way to start daemons, and the API exposed by default hides many system-level apis, making it easier to use, but a bit more complicated for those who have not been exposed to Linux. It is recommended that you learn more about Linux system calls and the kernel while learning Nodejs.

enjoy!