node.js如何通过进程名称检查进程是否正在运行?

use*_*178 13 node.js

我在linux中运行node.js,从node.js如何检查进程是否从进程名运行?最糟糕的情况我使用child_process,但想知道是否有更好的方法?

谢谢 !

Sam*_*Toh 10

您可以使用ps-node包.

https://www.npmjs.com/package/ps-node

var ps = require('ps-node');

// A simple pid lookup 
ps.lookup({
    command: 'node',
    psargs: 'ux'
    }, function(err, resultList ) {
    if (err) {
        throw new Error( err );
    }

    resultList.forEach(function( process ){
        if( process ){
            console.log( 'PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.command, process.arguments );
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

我相信你会看到这个例子.查看网站,他们有很多其他用途.试试看.

只是因为你没有绑定nodejs,你也可以从linux命令行ps -ef | grep "YOUR_PROCESS_NAME_e.g._nodejs"检查一个正在运行的进程.


d_s*_*lzi 8

以下应该有效.将基于操作系统生成进程列表,并且将针对所需程序解析该输出.该函数有三个参数,每个参数只是相应操作系统上的预期进程名称.

根据我的经验,ps-node需要花费太多的内存和时间来搜索进程.如果您计划经常检查流程,则此解决方案更好.

const exec = require('child_process').exec

function isRunning(win, mac, linux){
    return new Promise(function(resolve, reject){
        const plat = process.platform
        const cmd = plat == 'win32' ? 'tasklist' : (plat == 'darwin' ? 'ps -ax | grep ' + mac : (plat == 'linux' ? 'ps -A' : ''))
        const proc = plat == 'win32' ? win : (plat == 'darwin' ? mac : (plat == 'linux' ? linux : ''))
        if(cmd === '' || proc === ''){
            resolve(false)
        }
        exec(cmd, function(err, stdout, stderr) {
            resolve(stdout.toLowerCase().indexOf(proc.toLowerCase()) > -1)
        })
    })
}

isRunning('myprocess.exe', 'myprocess', 'myprocess').then((v) => console.log(v))
Run Code Online (Sandbox Code Playgroud)


mus*_*tin 7

d_scalzi回答的代码略有改进。具有回调而不是promise的功能,仅具有一个变量查询,并且具有switch而不是if / else。

const exec = require('child_process').exec;

const isRunning = (query, cb) => {
    let platform = process.platform;
    let cmd = '';
    switch (platform) {
        case 'win32' : cmd = `tasklist`; break;
        case 'darwin' : cmd = `ps -ax | grep ${query}`; break;
        case 'linux' : cmd = `ps -A`; break;
        default: break;
    }
    exec(cmd, (err, stdout, stderr) => {
        cb(stdout.toLowerCase().indexOf(query.toLowerCase()) > -1);
    });
}

isRunning('chrome.exe', (status) => {
    console.log(status); // true|false
})
Run Code Online (Sandbox Code Playgroud)

  • 投了反对票。这个函数在 mac 上总是返回 true,因为会有进程 'px -ax | grep {查询}' (2认同)

Chr*_*aks 7

这是其他答案的另一个版本,带有 TypeScript 和 promise:

export async function isProcessRunning(processName: string): Promise<boolean> {
  const cmd = (() => {
    switch (process.platform) {
      case 'win32': return `tasklist`
      case 'darwin': return `ps -ax | grep ${processName}`
      case 'linux': return `ps -A`
      default: return false
    }
  })()

  return new Promise((resolve, reject) => {
    require('child_process').exec(cmd, (err: Error, stdout: string, stderr: string) => {
      if (err) reject(err)

      resolve(stdout.toLowerCase().indexOf(processName.toLowerCase()) > -1)
    })
  })
}
Run Code Online (Sandbox Code Playgroud)
const running: boolean = await isProcessRunning('myProcess')
Run Code Online (Sandbox Code Playgroud)