import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; /**
* 操作系统命令工具
*/
public class ExecUtil {
private static final Logger logger = LogManager.getLogger(ExecUtil.class); /**
* 执行一条操作系统命令*/
public static CmdInfo exec(String command) {
logger.info("exec cmd begin: " + command);
try{
Process ps = Runtime.getRuntime().exec(command);
ps.waitFor();
StreamGobbler errorGobbler = new StreamGobbler(ps.getErrorStream());
errorGobbler.start();
StreamGobbler outGobbler = new StreamGobbler(ps.getInputStream());
outGobbler.start();
outGobbler.join();
CmdInfo info = new CmdInfo(ps.exitValue(), errorGobbler.getInfo(), outGobbler.getInfo());
return info;
}
catch(Exception e){
logger.info("exec cmd error: ", e);
throw new RuntimeException(e);
}
finally{
Runtime.getRuntime().runFinalization();
}
} public static CmdInfo exec(String[] command) { try{
Process ps = Runtime.getRuntime().exec(command);
ps.waitFor();
StreamGobbler errorGobbler = new StreamGobbler(ps.getErrorStream());
errorGobbler.start();
StreamGobbler outGobbler = new StreamGobbler(ps.getInputStream());
outGobbler.start();
outGobbler.join();
CmdInfo info = new CmdInfo(ps.exitValue(), errorGobbler.getInfo(), outGobbler.getInfo());
return info;
}
catch(Exception e){
logger.info("exec cmd error: ", e);
throw new RuntimeException(e);
}
finally{
Runtime.getRuntime().runFinalization();
// Runtime.getRuntime ().gc();
}
} public static boolean execCmdAndWait(String cmd, long timeout, TimeUnit unit) {
try {
Process process = Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", cmd});
process.waitFor(timeout, unit);
} catch (InterruptedException | IOException e) {
logger.error("exec cmd: " + cmd + " and wait " + timeout + " " + unit + " go wrong", e);
return false;
}
return true;
}/**
* 命令信息
*/
public static class CmdInfo implements Serializable { private static final long serialVersionUID = 7416244152344549775L;
private int exitValue;
private String errorInfo;
private String msgInfo; public CmdInfo(int exitValue, String errorStreamInfo, String inputStreamInfo) {
this.exitValue = exitValue;
this.errorInfo = errorStreamInfo;
this.msgInfo = inputStreamInfo;
} public String getErrorInfo() { return errorInfo;
}
public void setErrorInfo(String errorStreamInfo) { this.errorInfo = errorStreamInfo;
} public int getExitValue() { return exitValue;
} public void setExitValue(int exitValue) { this.exitValue = exitValue;
} public String getMsgInfo() { return msgInfo;
} public void setMsgInfo(String inputStreamInfo) { this.msgInfo = inputStreamInfo;
} } /**
* 处理Process的stdout和stderr的类
*/
static class StreamGobbler extends Thread { private InputStream is; private OutputStream os; private String info = ""; public StreamGobbler(InputStream is) { this(is, null);
} public StreamGobbler(InputStream is, OutputStream redirect) { this.is = is;
this.os = redirect;
} public String getInfo() { return this.info;
} /*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
public void run() { PrintWriter pw = null;
InputStreamReader isr = null;
BufferedReader br = null;
try{
pw = null;
if(os != null){
pw = new PrintWriter(os);
} isr = new InputStreamReader(is);//,"GBK"
br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null){
if(pw != null){
pw.println(line);
}
info += line + "\n";
// System.out.println (line);
}
if(pw != null){
pw.flush();
}
}
catch(IOException ioe){
throw new RuntimeException(ioe);
}
finally{
try{
if(pw != null){
pw.close();
}
if(isr != null){
isr.close();
}
if(br != null){
br.close();
}
}
catch(IOException e){
throw new RuntimeException(e);
}
}
}
} }

操作系统命令工具Util的更多相关文章

  1. 自己封装的poi操作Excel工具类

    自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...

  2. 自己的包poi操作Excel工具

    在前面的文章<使用poi读写Excel>中分享了一下poi操作Excel的简单演示样例.这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完毕的功能是:读取Excel.汇总E ...

  3. java日期操作常用工具

    java日期操作常用工具 package com..util; import java.sql.Timestamp; import java.text.SimpleDateFormat; import ...

  4. Java操作Redis工具类

    依赖 jar 包 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...

  5. Redis操作Set工具类封装,Java Redis Set命令封装

    Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...

  6. Redis操作List工具类封装,Java Redis List命令封装

    Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...

  7. Redis操作Hash工具类封装,Redis工具类封装

    Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...

  8. Redis操作字符串工具类封装,Redis工具类封装

    Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...

  9. java中文件操作的工具类

    代码: package com.lky.pojo; import java.io.BufferedReader; import java.io.BufferedWriter; import java. ...

随机推荐

  1. Photoshop基础照片美化

    自从有了“ps”以后,很多事情变成了可能,你可以出现在任何你想在的地方.而最基本的美化照片的功能,我想是很多同学学习PS的初衷.当你掌握了这门技术,很多人会对你刮目相看!今天小编就和大家分享一下ps的 ...

  2. 防止xss漏洞-编码转义

    用JS进行转义还是用PHP进行转义,最后存入数据库的是什么形式 比如:用户输入: <script>alrt(0);</script>那数据库里面存储的是源数据还是转以后的数据: ...

  3. Theorem、Proposition、Lemma和Corollary等的解释与区别

    Theorem:定理.是文章中重要的数学化的论述,一般有严格的数学证明. Proposition:可以翻译为命题,经过证明且interesting,但没有Theorem重要,比较常用. Lemma:一 ...

  4. Linux 服务器 个人常用操作命令记录

    1.实时查看log:tail -f 日志文件名 2.查看Apache运行的用户组或用户名:ps aux | grep httpd 或者是: ps -ef | grep httpd 3.查看cronta ...

  5. vue-router配置子路由

    1.改写App.vue 里面的代码 ,增加路由跳转,增加Hi页面1,Hi页面2的跳转 2.修改HI.vue 里面的内容,增加 <router-view class="aaa" ...

  6. Android开发 retrofit入门讲解 (RxJava模式)

    前言 retrofit除了正常使用以外,还支持RxJava的模式来使用,此篇博客讲解如何使用RxJava模式下的retrofit 依赖 implementation 'com.squareup.ret ...

  7. 安装rancher以及使用rancher倒入kubernetes集群和添加及管理集群

    1.docker安装rancher [root@rancher ~]# docker run -d --name rancher --restart=unless-stopped -p : -p : ...

  8. fatal error U1087: cannot have : and :: dependents for same target Stop.

    转自VC错误:http://www.vcerror.com/?p=72 问题描述: 完成后编译,发现有错误  D:\WinDDK\7600.16385.1\bin\makefile.new(7117) ...

  9. VS2010-MFC(MFC常用类:定时器Timer)

    转自:http://www.jizhuomi.com/software/232.html 前面一节讲了CTime类和CTimeSpan类的使用,本节继续讲与时间有关的定时器.定时器并不是一个类,主要考 ...

  10. 1 A+B问题

    原题网址: http://www.lintcode.com/zh-cn/problem/a-b-problem/# 给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符. 注意事项 你不需 ...