发送Ctrl-C以通过Java处理打开 [英] Send Ctrl-C to process open by Java

查看:142
本文介绍了发送Ctrl-C以通过Java处理打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows操作系统下。

Under Windows OS.

我通过Runtime.getRuntime()启动子进程.exec();
我想向进程发送一个ctrl-c来阻止它。

I start a sub-process via a Runtime.getRuntime().exec(); I want to send a "ctrl-c" to the process to stop it.

我做了一个小例子,使用Runtime.getRuntime()。exec (ping google.com -n 100000);
代码可以在那里找到: http://pastebin.com/f6315063f

I did a small example, with Runtime.getRuntime().exec("ping google.com -n 100000"); The code can be found there : http://pastebin.com/f6315063f

到目前为止,我尝试通过Process outputStream发送char'3'(ctrl-C字符)。

So far, I tried to send the char '3' (ctrl-C character) via Process outputStream.

这是一个代码:

 cmd = re.exec("ping google.com -n 10000"); 
 out = new BufferedWriter (new OutputStreamWriter(cmd.getOutputStream()));
 input =  new BufferedReader (new  InputStreamReader(cmd.getInputStream()));


 char ctrlBreak = (char)3;
 //Different testing way to send the ctrlBreak;
 out.write(ctrlBreak);
 out.flush();
 out.write(ctrlBreak+"\n");
 out.flush();

我不想杀死进程,我只想发送一个Ctrl-C信号。
我该怎么做?

I don't want to kill the process, I just want to send a Ctrl-C signal. How can I do that?

推荐答案

我相信Ctrl-C被shell捕获并转化为信号( SIGINT )发送到基础流程(在这种情况下,您的生成)过程)。

I believe Ctrl-C is caught by the shell and translated into a signal (SIGINT) which is sent to the underlying process (in this case your spawned process).

所以我认为您需要获取进程ID,然后将相应的信号发送到该进程。 此问题似乎非常相似,指向各种使用资源。

So I think you'll need to get the process id and then send the appropriate signal to that process. This question appears to be very similar and points to various resources of use.

这篇关于发送Ctrl-C以通过Java处理打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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