Java ProcessBuilder process.destroy()不会杀死WinXP中的子进程 [英] Java ProcessBuilder process.destroy() not killing child processes in winXP

查看:34
本文介绍了Java ProcessBuilder process.destroy()不会杀死WinXP中的子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java应用程序,它使用 ProcessBuilder 来准备操作系统命令,并给我一个 Process 对象.(实际的os命令是使用cygwin在ssh上进行rsync的操作.)

这在Windows中运行良好,但是,如果我想使用 process.destroy()停止该进程,它将不会杀死子ssh和rsync进程.....我必须使用Windows任务管理器手动杀死它们.

是否可以获取进程的 OutputStream 并以某种方式发送ctrl-c,然后再调用 destroy(); ?

如果有人对解决方法有任何想法,那就太好了.谢谢,D

解决方案

我还认为模拟Ctrl-C以完全杀死ssh是有问题的.

我要做的是以下方法之一.可以使用Windows命令找出谁是ssh的儿子(这有点麻烦,因为您需要知道当前的pid才能接收自己的子进程).我相信sysinternals的 pstools 是一个好命令行工具,使您可以跟踪孤立进程.请参见此示例,以通过控制Windows进程> taskList.exe (可以为您提供CSV格式的BTW输出)或通过执行特殊的VBScript.

第二种方法是使用Java库(例如 winp 来执行和控制)ssh进程.我相信您可以列出其所有子级,如果发送正确的消息不足以将其强行杀死.这将是我的首选方法.请注意, killRecursively 方法完全可以满足您的需求.

请注意,这些方法不应仅呈现您的应用程序窗口.您可以将它们封装在可以在Windows和linux计算机上不同地运行的类中.

请注意,我并没有尝试对Windows进程进行细粒度的控制,所以我不确定我找到的解决方案将有多成熟.

I have a java app that uses ProcessBuilder to prepare an operating system command and gives me a Process object. (The actual os command is rsync over ssh using cygwin).

This is working well in Windows, however if I want to stop the process using process.destroy() it will not kill the child ssh and rsync processes..... I have to manually kill them using the windows task manager..

Is it possible to get the OutputStream of the process and send a ctrl-c somehow before I call destroy();?

If anyone has any ideas on a workaround it would be great. Thanks, D

解决方案

I also think that emulating Ctrl-C in order to kill ssh entirely is problematic.

What I would do, is one of the following approaches. Either use windows commands to find out who are ssh's sons (which is a little bit problematic, since you need to know your current pid in order to recieve your own children-processes). I believe pstools of sysinternals is a good command-line tool that should enable you to track orphan processes. See this example for controlling windows processes either with taskList.exe (which can give you its output in CSV format BTW) or by executing a special VBScript.

The second approach is using a java library such as winp to execute and control the ssh process. I believe you'd be able to list all its children and forcibly kill them if sending the correct message would not suffice. This would be my preferred approach. Please note that the killRecursively method does exactly what you want.

Please note that those approaches should not render your application windows only. You can encapsulate those in a class that would run differently on Windows and linux machines.

Please note I didn't try to gain a fine-grained control on windows processes with, so I'm not sure how mature would those solutions I found are.

这篇关于Java ProcessBuilder process.destroy()不会杀死WinXP中的子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆