Nodejs如何通过PID独立检查进程是运行?
Nodejs how to check independently a process is running by PID?
我正在使用 child_process
生成子进程并从中获取 return PID。我需要通过它的 PID 来管理这个子进程。下面是我的代码:
const childProcess = require('child_process');
let options = ['/c', arg1, arg2];
const myProcess = childProcess.spawn('cmd.exe', options, {
detached: false,
shell: false
});
let pid = myProcess.pid;
在 运行 时间里,我想使用 PID 从外部独立验证 进程是否 运行ning(完成/终止) .我想知道如何执行此操作以及在 Nodejs 中进行此验证的最佳方法是什么?我在 Windows 环境中 运行ning 应用程序。
感谢任何建议,谢谢。
这里有一个代码片段作为参考
win32 (cond) {
return new Promise((resolve, reject) => {
const cmd = 'WMIC path win32_process get Name,Processid,ParentProcessId,Commandline,ExecutablePath'
const lines = []
const proc = utils.spawn('cmd', ['/c', cmd], { detached: false, windowsHide: true })
proc.stdout.on('data', data => {
lines.push(data.toString())
})
proc.on('close', code => {
if (code !== 0) {
return reject(new Error('Command \'' + cmd + '\' terminated with code: ' + code))
}
let list = utils.parseTable(lines.join('\n'))
.filter(row => {
if ('pid' in cond) {
return row.ProcessId === String(cond.pid)
} else if (cond.name) {
if (cond.strict) {
return row.Name === cond.name || (row.Name.endsWith('.exe') && row.Name.slice(0, -4) === cond.name)
} else {
// fix #9
return matchName(row.CommandLine || row.Name, cond.name)
}
} else {
return true
}
})
.map(row => ({
pid: parseInt(row.ProcessId, 10),
ppid: parseInt(row.ParentProcessId, 10),
// uid: void 0,
// gid: void 0,
bin: row.ExecutablePath,
name: row.Name,
cmd: row.CommandLine
}))
resolve(list)
})
})
},
来自https://github.com/yibn2008/find-process/blob/master/lib/find_process.js
可能这会有所帮助,npm 模块名为 is-运行 https://npmjs.org/package/is-running 正如这里提到的 -
如果想知道子进程什么时候退出,可以查看exit
event
const { spawn } = require('child_process');
const bat = spawn('cmd.exe', ['/c', 'my.bat']);
bat.stdout.on('data', (data) => {
console.log(data.toString());
});
bat.stderr.on('data', (data) => {
console.log(data.toString());
});
bat.on('exit', (code) => {
console.log(`Child exited with code ${code}`);
});
我找到了 is-running 模块建议的解决方案。但我不想为此目的将新模块安装到我的项目中,所以我创建了自己的 checkRunning()
函数,如下所示:
// Return true if process following pid is running
checkRunning(pid) {
try {
return process.kill(pid, 0);
} catch (error) {
console.error(error);
return error.code === 'EPERM';
}
}
根据有关 process.kill(pid[, signal]) 的 Nodejs 文档,我可以使用 process.kill()
检查特定 signal
参数是否为值 0 的进程是否存在(不是杀死进程作为函数名)。
我复制的文件说:
As a special case, a signal of 0 can be used to test for the existence of a process
这里是相关主题最好的包
仅通过 pid 检查
https://www.npmjs.com/package/is-running
按 pid、名称模式和不同方式搜索包:
https://www.npmjs.com/package/find-process
我正在使用 child_process
生成子进程并从中获取 return PID。我需要通过它的 PID 来管理这个子进程。下面是我的代码:
const childProcess = require('child_process');
let options = ['/c', arg1, arg2];
const myProcess = childProcess.spawn('cmd.exe', options, {
detached: false,
shell: false
});
let pid = myProcess.pid;
在 运行 时间里,我想使用 PID 从外部独立验证 进程是否 运行ning(完成/终止) .我想知道如何执行此操作以及在 Nodejs 中进行此验证的最佳方法是什么?我在 Windows 环境中 运行ning 应用程序。
感谢任何建议,谢谢。
这里有一个代码片段作为参考
win32 (cond) {
return new Promise((resolve, reject) => {
const cmd = 'WMIC path win32_process get Name,Processid,ParentProcessId,Commandline,ExecutablePath'
const lines = []
const proc = utils.spawn('cmd', ['/c', cmd], { detached: false, windowsHide: true })
proc.stdout.on('data', data => {
lines.push(data.toString())
})
proc.on('close', code => {
if (code !== 0) {
return reject(new Error('Command \'' + cmd + '\' terminated with code: ' + code))
}
let list = utils.parseTable(lines.join('\n'))
.filter(row => {
if ('pid' in cond) {
return row.ProcessId === String(cond.pid)
} else if (cond.name) {
if (cond.strict) {
return row.Name === cond.name || (row.Name.endsWith('.exe') && row.Name.slice(0, -4) === cond.name)
} else {
// fix #9
return matchName(row.CommandLine || row.Name, cond.name)
}
} else {
return true
}
})
.map(row => ({
pid: parseInt(row.ProcessId, 10),
ppid: parseInt(row.ParentProcessId, 10),
// uid: void 0,
// gid: void 0,
bin: row.ExecutablePath,
name: row.Name,
cmd: row.CommandLine
}))
resolve(list)
})
})
},
来自https://github.com/yibn2008/find-process/blob/master/lib/find_process.js
可能这会有所帮助,npm 模块名为 is-运行 https://npmjs.org/package/is-running 正如这里提到的 -
如果想知道子进程什么时候退出,可以查看exit
event
const { spawn } = require('child_process');
const bat = spawn('cmd.exe', ['/c', 'my.bat']);
bat.stdout.on('data', (data) => {
console.log(data.toString());
});
bat.stderr.on('data', (data) => {
console.log(data.toString());
});
bat.on('exit', (code) => {
console.log(`Child exited with code ${code}`);
});
我找到了 is-running 模块建议的解决方案。但我不想为此目的将新模块安装到我的项目中,所以我创建了自己的 checkRunning()
函数,如下所示:
// Return true if process following pid is running
checkRunning(pid) {
try {
return process.kill(pid, 0);
} catch (error) {
console.error(error);
return error.code === 'EPERM';
}
}
根据有关 process.kill(pid[, signal]) 的 Nodejs 文档,我可以使用 process.kill()
检查特定 signal
参数是否为值 0 的进程是否存在(不是杀死进程作为函数名)。
我复制的文件说:
As a special case, a signal of 0 can be used to test for the existence of a process
这里是相关主题最好的包
仅通过 pid 检查
https://www.npmjs.com/package/is-running
按 pid、名称模式和不同方式搜索包:
https://www.npmjs.com/package/find-process