node.jspidplatform-independent

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


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 /proc or the Windows equivalent.

This could be done either synchronously:

if (isAlive(pid)) { //do stuff }

Or asynchronously:

getProcessStatus(pid, function(status) {
    if (status === "alive") { //do stuff }
}

Note that I'm hoping to find a solution for this that works with an arbitrary system PID , not just the PID of a running Node.js process.


Solution

  • 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