如何使用Node.js检查任意PID是否正在运行? [英] How to check if an arbitrary PID is running using Node.js?
问题描述
是否有某种方法可以使用Node.js检查系统上是否正在运行或运行任意PID?假定Node.js脚本具有读取/proc
或Windows等效文件的适当权限.
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.
这可以同步完成:
if (isAlive(pid)) { //do stuff }
或异步:
getProcessStatus(pid, function(status) {
if (status === "alive") { //do stuff }
}
请注意,我希望为此找到一种解决方案,该解决方案可以与任意系统PID配合使用,而不是只是运行中的Node.js进程的PID.
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.
推荐答案
我还需要检查项目中是否运行了pid.我参加了这个答案使用kill -0 <PID>
并将其包装在名为正在运行的模块中
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
这篇关于如何使用Node.js检查任意PID是否正在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!