Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Under Windows OS.

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

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

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

Here is a bit of code:

 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+"
");
 out.flush();

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
928 views
Welcome To Ask or Share your Answers For Others

1 Answer

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).

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...