Home  >  Article  >  Web Front-end  >  How to query process PID in Node.js

How to query process PID in Node.js

PHPz
PHPzOriginal
2023-04-26 09:11:171658browse

Node.js is an open source JavaScript running environment based on the Chrome V8 engine that can run JavaScript on the server side. It adopts an event-driven, non-blocking I/O model, making it ideal for lightweight, efficient, real-time data interaction applications.

When developing Node.js applications, sometimes we need to query the PID of the current process, that is, the process ID. PID is a numeric type that uniquely identifies a process in the system. Normally, we can query the PID of a process through shell commands or system APIs. This article will introduce how to query the process PID in Node.js.

Querying PID through process object

In Node.js, you can use the process object to obtain information about the current process, including the PID of the process. The process object is one of the global objects of Node.js and we can access it at any time. The following is a sample code to get the PID of the current process:

console.log('当前进程的 PID 是:', process.pid);

The above code will output the PID of the current Node.js process.

Querying PID by process name

In addition to querying the PID of the current process, sometimes we also need to query the PID of a specific process. At this time, the PID can be queried by the process name. In Linux/macOS, you can use the shell command ps to query process information. In Windows, you can use the tasklist command to query process information. The following is a sample code to obtain the PID of a specified process:

Linux/macOS

const { exec } = require('child_process');

exec('pgrep node', (err, stdout, stderr) => {
  if (!err && stdout) {
    console.log('当前 Node.js 进程的 PID 是:', stdout.trim());
  }
});

The above code will query the currently running Node.js process through the shell command pgrep node PID and output the results.

Windows

const { exec } = require('child_process');

exec('tasklist /FI "IMAGENAME eq node.exe" /NH', (err, stdout, stderr) => {
  if (!err && stdout) {
    const pids = stdout.trim().split('\n').map(line => {
      const columns = line.trim().split(/\s+/);
      return parseInt(columns[1]);
    }).filter(pid => !isNaN(pid));
    console.log('当前 Node.js 进程的 PID 是:', pids[0]);
  }
});

The above code will query the PID of the currently running Node.js process through the shell command tasklist /FI "IMAGENAME eq node.exe" /NH. and output the result.

Summary

This article introduces two methods for Node.js to query process PID: querying the current process PID through the process object, and querying the specified process PID through the process name. Both methods are very useful for Node.js applications that need to manage processes.

The above is the detailed content of How to query process PID in Node.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn