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

放肆的年华 提交于 2019-12-09 02:17:07

问题


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.


回答1:


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




回答2:


Seems like 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."




回答3:


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



来源:https://stackoverflow.com/questions/14390930/how-to-check-if-an-arbitrary-pid-is-running-using-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!