[英]Killing java (Leiningen) processes from Node
I'm launching a bunch of java
processes from NodeJS (via child_process.spawn
). 我正在通过child_process.spawn
(通过child_process.spawn
)启动一堆java
进程。 Technically, I'm launching Leiningen (a Clojure build tool, lein
). 从技术上讲,我正在启动Leiningen(Clojure构建工具lein
)。 Later I try to kill them and all their children. 后来我试图杀死他们和他们所有的孩子。 But it almost never works and all I get is an Activity Monitor (OSX) filled with java
s. 但是它几乎永远无法工作,我得到的只是一个充满java
的活动监视器(OSX)。
I kill them by first running thisProcess.kill(leinProcess.pid);
我先运行thisProcess.kill(leinProcess.pid);
杀死它们thisProcess.kill(leinProcess.pid);
(defaults to SIGTERM), waiting 1 second and then calling leinProcess.kill("SIGKILL");
(默认为SIGTERM),等待1秒钟,然后调用leinProcess.kill("SIGKILL");
. 。
All the processes and the main process are run under the same user. 所有进程和主进程都在同一用户下运行。
Running killall -9 java
from command line works. 从命令行运行killall -9 java
。
The problem was with orphaned java
sub-sub-processes. 问题出在孤立的java
子子进程上。 See this readme for an explanation and solution: https://github.com/indexzero/ps-tree 请参阅此自述文件以获取解释和解决方案: https : //github.com/indexzero/ps-tree
I've been doing the same thing, launching multiple instances of lein run
on different microservices. 我一直在做同样的事情,启动在不同微服务上lein run
多个lein run
实例。 In order to kill them, I've been using npm install tree-kill
为了杀死他们,我一直在使用npm install tree-kill
var kill = require('tree-kill');
var spawn = require('child_process').spawn;
var proc = spawn("lein", ["run"], {cwd: './some-dir', detached: true});
setTimeout(function(){kill(proc.pid); console.log('Take that!');}, 5000);
var kill = require('tree-kill');
var chalk = require('chalk');
exports.killIfAlive= function(pid) {
try {
kill(pid);
}
catch(ex) {
console.error(ex);
}
};
exports.kill = function(projects) {
var pids = exports.readPIDFile();
projects.forEach(function(project) {
if (pids[project]) {
console.log('Killing', chalk.cyan(project), chalk.magenta('(pid:', pids[project], ')'));
exports.killIfAlive(pids[project]);
}
else {
console.log(chalk.cyan(project), chalk.grey('is not running.'));
}
delete pids[project];
});
return exports.writePIDFile(pids);
};
After starting each project, I store its pid into a simple object like {project1: 12352, project2: 50943}
and then write that to the file system. 启动每个项目后,我将其pid存储到一个简单的对象中,例如{project1: 12352, project2: 50943}
,然后将其写入文件系统。 Since I run spawn('lein' ... etc)
with the detached
option, my current node process can die without taking out my leiningen processes. 因为我使用detached
选项运行spawn('lein' ... etc)
,所以我当前的节点进程可以死掉而无需取出我的leiningen进程。 Whenever I revive my node process, it can use the pid file to look up and terminate one or more projects. 每当我恢复节点进程时,它都可以使用pid文件查找并终止一个或多个项目。 When taking this approach, there is the chance for a race condition such that your lein process with the given pid has already died and a new process has started under that pid which I'll leave for someone else to handle. 采用这种方法时,可能会出现竞争状况,以至于带有给定pid的lein过程已经死亡,并且在该pid下开始了一个新过程,我将留给别人处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.