执行Linux命令在Java中 [英] Executing Linux Command in Java
问题描述
我想在Java中以杀死这是通过Java执行bash脚本的催生过程中执行以下命令:
I'm trying to execute the following Command in Java in order to kill the spawned process of bash script which is executed through java :
kill $(pgrep -P $(pgrep -P 5537))
我使用Apache Exec的命令行构建命令,但它是在这里使用的ProcessBuilder没有什么不同。因此,这里是我迄今为止:
I'm using apache Commons Exec Commandline to build the Command but it's no different to using ProcessBuilder here. So here is what I have so far:
CommandLine cmdLine = new CommandLine("bash");
cmdLine.addArgument("-c");
cmdLine.addArgument("kill $(pgrep -P $(pgrep -P "+pid+"))");
我得到的错误
bash: $'kill 7940\n7941\n7942\n7943': Command not found.
通常我现在会尝试获取新行该命令,但它也不起作用杀死只是一个单一的过程,因为当时我得到的错误:
Normally I would now try to get the newlines out of the Command but it also doesn't work to kill just a single process because then I get the error :
bash: kill 7980: Command not found.
一,一方面我需要使用bash中使用变量,另一方面,我不能使用它,因为杀不能用它来执行...
One the one hand I need to use bash to use the variables and on the other hand I can't use it because kill can't be executed with it...
推荐答案
首先杀-9 pidnumber
为什么你需要的bash的变量? java的时候给你字符串存储变量?
Why would you need the bash variables? when java gives you strings to store variables?
第三,为什么不尝试 System.Runtime.getRuntime()。EXEC()
?
Thirdly why not try System.Runtime.getRuntime().exec()
?
另外你有权杀任务?试图须藤杀死-9 PID
?
Also do you have permissions to kill the task? tried sudo kill -9 pid
?
这篇关于执行Linux命令在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!