Send Control-C to a process running through Runtime.getRuntime (). Exec ("su");

I am developing an application that should run the command as root, so I use:

process = Runtime.getRuntime().exec("su");

Then I start the te process with

os = new DataOutputStream(process.getOutputStream());

os.writeBytes("tcpdump\n");

When I need to complete the process os.writeBytes("exit\n");, it does not work, but process.waitFor();receives a lock and the process does not end. I need to send Control-C to a process to stop it, but I don't know how to do it.

Thank.

+3
source share
3 answers

Find out if the API has a kill () method, and use this method to send a SIGINT signal to the target process.

+1
source

github Chainfire Shell, root. Threads, , , .

:

if(Shell.SU.available()){
   Shell.SU.run("commandAsRoot"); //Command executed as root
else{
   System.out.println("su not found");

, , su , ( ) .

: How-To SU

+1

, ctrl-c.

Process interrupt = Runtime.getRuntime().exec("su");
ios = new DataOutputStream(interrupt.getOutputStream());

ios.writeBytes("pkill -SIGINT tcpdump");
ios.flush();
ios.close();
interrupt.waitFor();

, tcpdump, , , pidof grep. , . , .

0
source

Source: https://habr.com/ru/post/1793877/


All Articles