java shell 命令
- package com.taskschedule.util;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- import com.taskschedule.util.StreamGobbler;
- /**
- *
- * @author baoyou E-mail:curiousby@163.com
- * @version 2016年11月2日 下午1:54:49
- * desc:
- */
- public class ShellProcess {
- private static ShellProcess instance;
- public static ShellProcess getInstance() {
- if (instance == null) {
- return new ShellProcess();
- }
- return instance;
- }
- 下载
- /**
- * 执行相应shell脚本
- * @param args 执行脚本的参数,[0]path:shell脚本路径;[1~n]脚本入参
- * @return 返回码,0:成功 1:失败
- */
- public int runShell(String[] args) {
- int runRes = SystemGlobal.FAILED;
- try {
- Process process = Runtime.getRuntime().exec(args);//调用相应shell脚本
- new StreamGobbler(process.getInputStream(), StreamGobbler.INFO).start();
- new StreamGobbler(process.getErrorStream(), StreamGobbler.ERROR).start();
- runRes = process.waitFor();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return runRes;
- }
- /**
- * 执行相应shell命令
- * @param cmd 执行的命令
- * @return 返回码,0:成功 1:失败
- */
- public int runShell(String cmd) {
- int runRes = SystemGlobal.FAILED;
- try {
- Process process = Runtime.getRuntime().exec(cmd);//执行shell命令
- new StreamGobbler(process.getInputStream(), StreamGobbler.INFO).start();
- new StreamGobbler(process.getErrorStream(), StreamGobbler.ERROR).start();
- runRes = process.waitFor();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return runRes;
- }
- /**
- * 自动根据运行时环境执行shell命令
- * @param args shell命令以空格分割后的list
- * @return 返回码,0:成功 1:失败
- */
- public int runShell(List<String> args) {
- int runRes = SystemGlobal.FAILED;
- try {
- ProcessBuilder pb = new ProcessBuilder(args);
- Process process = pb.start();
- new StreamGobbler(process.getInputStream(), StreamGobbler.INFO).start();
- new StreamGobbler(process.getErrorStream(), StreamGobbler.ERROR).start();
- runRes = process.waitFor();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return runRes;
- }
- 下载
- /**
- * 执行shell命令,并获取返回结果
- * @param args shell命令以空格分割后的list
- * @return 执行shell命令后的返回结果(按行分割后的list),如果发生异常,返回空List
- */
- public List<String> runShellWithResult(List<String> args) {
- List<String> results = new ArrayList<String>();
- try {
- ProcessBuilder pb = new ProcessBuilder(args);
- Process process = pb.start();
- BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
- String data = null;
- while ((data = br.readLine()) != null) {
- results.add(data);
- }
- new StreamGobbler(process.getInputStream(), StreamGobbler.INFO).start();
- new StreamGobbler(process.getErrorStream(), StreamGobbler.ERROR).start();
- process.waitFor();
- } catch (Exception e) {
- e.printStackTrace();
- return new ArrayList<String>();
- }
- return results;
- }
- }
- package com.taskschedule.util;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import org.apache.log4j.Logger;
- /**
- *
- * @author baoyou E-mail:curiousby@163.com
- * @version 2016年11月2日 下午1:54:57
- * desc:
- */
- public class StreamGobbler extends Thread {
- private InputStream is;
- private int type;
- private static Logger logger = Logger.getLogger(StreamGobbler.class);
- public static final int INFO = 0;
- public static final int ERROR = 1;
- public StreamGobbler(InputStream is, int type) {
- this.is = is;
- this.type = type;
- }
- 下载
- @Override
- public void run() {
- try {
- InputStreamReader isr = new InputStreamReader(is);
- BufferedReader br = new BufferedReader(isr);
- String line = null;
- while ((line = br.readLine()) != null) {
- if (type == INFO) {
- logger.info(line);
- } else if (type == ERROR) {
- logger.error(line);
- }
- }
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
- }