Runtime.getRuntime().exec如何执行多行命令,坐等方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Runtime.getRuntime().exec如何执行多行命令,坐等方法相关的知识,希望对你有一定的参考价值。
如果执行一条命令,我可以使用:比如:Runtime.getRuntime().exec("cat /sys/class/key_read");如果有三条命令,比如:echo nand3 > /sys/class/versionecho hdcp > /sys/class/key_namecat /sys/class/key_read如果分三次执行,结果只能响应最后一句,如何同时执行这三句命令坐等。。。
logcat输出为null,表明代码还是有问题。echo这句是把字符串nand3输出到version中 参考技术A 直接上代码好了,关键是先启动sh进程,然后获取output流,把每个命令通过流写入/**
* @param pathOrCommand 脚本路径或者命令
* @return
*/
public static List<String> exceShell(List<String> pathOrCommand)
List<String> result = new ArrayList<>();
if (CollectionUtils.isEmpty(pathOrCommand)) return result;
try
// 执行脚本
Process ps = Runtime.getRuntime().exec("/bin/sh");
OutputStream os = ps.getOutputStream();
for (String cmd :pathOrCommand)
os.write((cmd+"\n").getBytes(StandardCharsets.UTF_8));
os.flush();
os.write(("exit\n").getBytes(StandardCharsets.UTF_8));
os.flush();
// 等待一分钟后超时
if(!ps.waitFor(1L, TimeUnit.MINUTES))
ps.destroy();
BufferedInputStream in = new BufferedInputStream(ps.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null)
result.add(line);
BufferedReader errorBr = new BufferedReader(new InputStreamReader(new BufferedInputStream(ps.getErrorStream())));
while ((line = errorBr.readLine()) != null)
result.add(line);
in.close();
br.close();
os.close();
if (0 != ps.exitValue())
System.out.println("call shell failed. error code is :" + ps.exitValue());
result.add("call shell failed. error code is :" + ps.exitValue());
catch (Exception e)
e.printStackTrace();
result.add("call shell failed. error message is :" + e.getMessage());
return result;
java中Runtime.getRuntime().exec()的坑,会造成阻塞的解决
synchronized (this) {
process = Runtime.getRuntime().exec(cmds);
}
//记录进程缓存错误信息
final StringBuffer errorLog = new StringBuffer();
//获取执行进程的错误流
final InputStream errorStream = process.getErrorStream();
final InputStream inputStream = process.getInputStream();
//处理InputStream的线程
new Thread() {
public void run() {
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
try {
while ((line = in.readLine()) != null && !errorLog.toString().contains("ERROR")) {
if (line != null) {
errorLog.append(line);
}
}
} catch (IOException e) {
throw new RuntimeException("[shell exec error]:" + errorLog, e);
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
//处理errorStream的线程
new Thread() {
@Override
public void run() {
BufferedReader err = new BufferedReader(new InputStreamReader(errorStream));
String line = null;
try {
while ((line = err.readLine()) != null && !errorLog.toString().contains("ERROR")) {
if (line != null) {
errorLog.append(line);
}
}
} catch (IOException e) {
throw new RuntimeException("[shell exec error]:" + errorLog, e);
} finally {
try {
errorStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
logger.info("等待shell脚本执行完成");
Thread.sleep(1000);
//异常终止
if (errorLog != null && errorLog.length() > 0 && errorLog.toString().contains("ERROR")) {
dispatchLogger.error("[shell exec error]:" + errorLog);
throw new RuntimeException("[shell exec error]:" + errorLog);
}
process.waitFor(); //等待shell脚本执行完成
以上是关于Runtime.getRuntime().exec如何执行多行命令,坐等方法的主要内容,如果未能解决你的问题,请参考以下文章
Android Runtime.getRuntime().exec
如何与 Runtime.getRuntime().exec(command) 程序进行交互?
在循环中运行“Runtime.getRuntime()。exec()”
Runtime.getRuntime.exec()执行java进程失败
无法从 Java 进程(Runtime.getRuntime().exec() 或 ProcessBuilder)读取 InputStream