java执行shell脚本

时间:2023-01-30 18:02:28

http://blog.csdn.net/arkblue/article/details/7897396

一、直接调用版

使用到Process和Runtime两个类,返回值通过Process类的getInputStream()方法获取

[plain] view plain copy
  1. package ark;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6. import java.util.ArrayList;  
  7. import java.util.List;  
  8.   
  9. public class ReadCmdLine {  
  10.     public static void main(String args[]) {  
  11.         Process process = null;  
  12.         List<String> processList = new ArrayList<String>();  
  13.         try {  
  14.             process = Runtime.getRuntime().exec("ps -aux");  
  15.             BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));  
  16.             String line = "";  
  17.             while ((line = input.readLine()) != null) {  
  18.                 processList.add(line);  
  19.             }  
  20.             input.close();  
  21.         } catch (IOException e) {  
  22.             e.printStackTrace();  
  23.         }  
  24.   
  25.         for (String line : processList) {  
  26.             System.out.println(line);  
  27.         }  
  28.     }  
  29. }  

调用shell脚本,判断是否正常执行,如果正常结束,Process的waitFor()方法返回0

[java] view plain copy
  1. public static void callShell(String shellString) {  
  2.     try {  
  3.         Process process = Runtime.getRuntime().exec(shellString);  
  4.         int exitValue = process.waitFor();  
  5.         if (0 != exitValue) {  
  6.             log.error("call shell failed. error code is :" + exitValue);  
  7.         }  
  8.     } catch (Throwable e) {  
  9.         log.error("call shell failed. " + e);  
  10.     }  
  11. }  
二、异步版

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Properties;

public class ShellExec {

public static void execShell(String command) {
InputStreamReader stdISR = null;
InputStreamReader errISR = null;
Process process = null;
//String command = "/home/Lance/workspace/someTest/testbash.sh";
long timeout = 10 * 1000;
try {
process = Runtime.getRuntime().exec(command);

//CommandStreamGobbler errorGobbler = new CommandStreamGobbler(
//process.getErrorStream(), command, "ERR");
//CommandStreamGobbler outputGobbler = new CommandStreamGobbler(
//process.getInputStream(), command, "STD");

//errorGobbler.start();
// 必须先等待错误输出ready再建立标准输出
//while (!errorGobbler.isReady()) {
//Thread.sleep(10);
//}
//outputGobbler.start();
//while (!outputGobbler.isReady()) {
//Thread.sleep(10);
//}

CommandWaitForThread commandThread = new CommandWaitForThread(
process);
commandThread.start();

long commandTime = new Date().getTime();
long nowTime = new Date().getTime();
boolean timeoutFlag = false;
while (!commandThread.isFinish()) {
if (nowTime - commandTime > timeout) {
timeoutFlag = true;
break;
} else {
Thread.sleep(1000);
nowTime = new Date().getTime();
}
}
if (timeoutFlag) {
// 命令超时
//errorGobbler.setTimeout(1);
//outputGobbler.setTimeout(1);
System.out.println("正式执行命令:" + command + "超时");
}

//while (true) {
//if (errorGobbler.isReadFinish() && outputGobbler.isReadFinish()) {
//break;
//}
//Thread.sleep(10);
//}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
} finally {
if (process != null) {
process.destroy();
}
}
}
public class CommandWaitForThread extends Thread {private Process process;private boolean finish = false;private int exitValue = -1;public CommandWaitForThread(Process process) {this.process = process;}public void run() {try {this.exitValue = process.waitFor();} catch (InterruptedException e) {e.printStackTrace();} finally {finish = true;}}public boolean isFinish() {return finish;}public void setFinish(boolean finish) {this.finish = finish;}public int getExitValue() {return exitValue;}}