[英]NodeJS- get process ID from within shell script exec
I have some NodeJS code which runs a shell script using child_process.exec()
. 我有一些NodeJS代码使用child_process.exec()
运行shell脚本。 Within this shell script I run a program someProgram
. 在这个shell脚本中,我运行一个程序someProgram
。 What I would like to do is get the PID of someProgram
and pass that back into my Javascript code so I can later kill that specific process with another child_process.exec()
call. 我想要做的是获取someProgram
的PID并将其传递回我的Javascript代码,以便稍后我可以使用另一个child_process.exec()
调用来杀死该特定进程。 Is this possible? 这可能吗?
var exec = require('child_process').exec;
var pid = {};
exec('. ./script.sh', function(err, stdout, stderr) {
console.log(stdout);
setTimeout(function() {
exec('kill ' + pid, function(err, stdout, stderr) {
console.log(stdout);
});
}, 6000);
});
exec('pgrep -f someProgram', function(err, stdout, stderr) {
console.log('stdout' + stdout);
pid = stdout;
console.log('pid ' + pid);
});
just note that the bottom exec would run concurrently. 请注意底部exec将同时运行。 You could use this in a gulpfile, etc. 你可以在gulpfile等中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.