将Control-C发送到通过Runtime.getRuntime()。exec(“su”)运行的进程;

时间:2011-02-22 14:13:05

标签: android exec root su control-c

我正在开发一个需要以root用户身份运行命令的应用程序,所以我使用:

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

然后我用:

启动te流程
os = new DataOutputStream(process.getOutputStream());

os.writeBytes("tcpdump\n");

当我需要完成该过程时os.writeBytes("exit\n");无效并且process.waitFor();被阻止且该过程无法完成。我需要将Control-C发送到进程来阻止它,但我不知道如何做到这一点。

感谢。

3 个答案:

答案 0 :(得分:1)

找出API是否在某处使用kill()方法并使用该方法将SIGINT信号发送到目标进程。

答案 1 :(得分:1)

在他的github上,Chainfire提供了sample implementationShell类,您可以使用它来以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

答案 2 :(得分:0)

在想要发出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命令查找特定的进程ID。如果它对您有用,请接受答案。如果您遇到问题,请在评论中告诉我。

相关问题