How to check if an arbitrary PID is running using Node.js?

后端 未结 3 1175
没有蜡笔的小新
没有蜡笔的小新 2021-01-07 18:30

Is there some way to check if an arbitrary PID is running or alive on the system, using Node.js? Assume that the Node.js script has the appropriate permissions to read

相关标签:
3条回答
  • 2021-01-07 19:13

    I needed to check for running pid's in a project as well. I took this answer of using kill -0 <PID> and wrapped it up in a module called is-running https://npmjs.org/package/is-running

    npm install is-running

    0 讨论(0)
  • 2021-01-07 19:16

    You can call process.kill(pid, 0) and wrap it up in a try/catch.

    http://nodejs.org/api/process.html#process_process_kill_pid_signal -

    "Will throw an error if target does not exist, and as a special case, a signal of 0 can be used to test for the existence of a process."

    Example:

    function pidIsRunning(pid) {
      try {
        process.kill(pid, 0);
        return true;
      } catch(e) {
        return false;
      }
    }
    
    0 讨论(0)
  • 2021-01-07 19:23

    I'm not an expert here, but could you not spawn a child process to check the status with a command line tool?

    0 讨论(0)
提交回复
热议问题