操作系统命令工具Util
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的更多相关文章
- 自己封装的poi操作Excel工具类
		
自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...
 - 自己的包poi操作Excel工具
		
在前面的文章<使用poi读写Excel>中分享了一下poi操作Excel的简单演示样例.这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完毕的功能是:读取Excel.汇总E ...
 - java日期操作常用工具
		
java日期操作常用工具 package com..util; import java.sql.Timestamp; import java.text.SimpleDateFormat; import ...
 - Java操作Redis工具类
		
依赖 jar 包 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...
 - Redis操作Set工具类封装,Java Redis Set命令封装
		
Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...
 - Redis操作List工具类封装,Java Redis List命令封装
		
Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...
 - Redis操作Hash工具类封装,Redis工具类封装
		
Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...
 - Redis操作字符串工具类封装,Redis工具类封装
		
Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...
 - java中文件操作的工具类
		
代码: package com.lky.pojo; import java.io.BufferedReader; import java.io.BufferedWriter; import java. ...
 
随机推荐
- .NET Core 3.0之深入源码理解Startup的注册及运行
			
原文:.NET Core 3.0之深入源码理解Startup的注册及运行 写在前面 开发.NET Core应用,直接映入眼帘的就是Startup类和Program类,它们是.NET Core应用程 ...
 - 幂等 zuul的Filter实现
			
通过zuul的过滤器 filter实现 //app 幂等过滤 @SuppressWarnings("all") @Order(Ordered.HIGHEST_PRECEDENCE) ...
 - Leetcode207. Course Schedule课程表
			
现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1] 给定课程总量以及它 ...
 - python open函数关于w+ r+ 读写操作的理解(转)
			
r 只能读 (带r的文件必须先存在)r+ 可读可写 不会创建不存在的文件.如果直接写文件,则从顶部开始写,覆盖之前此位置的内容,如果先读后写,则会在文件最后追加内容.w+ 可读可写 如果文件存在 则覆 ...
 - BeanPostProcessor原理--使用讲解
			
<Spring源码解析>笔记 BeanPostProcessor原理学习 在学习BeanPostProcessor的原理学习完之后,对Spring如何使用充满好奇,尝试使用例子进行理解,以 ...
 - datetime模块练习
			
#_author:来童星#date:2019/12/6#1.获取当前日期import datetimeprint(datetime.date.today())# 2019-12-06#2.使用toda ...
 - HTML 参考手册web
			
{ https://www.w3school.com.cn/tags/index.asp }
 - 免费带你体验阿里巴巴旗舰大数据计算产品MaxCompute
			
什么是MaxCompute? 众所周知,MaxCompute是阿里云推出的承载EB级的数据存储能力,百PB级的单日计算能力,公共云覆盖国内外十几个国家和地区,专有云包含城市大脑在内部署超过100+套的 ...
 - hadoop高可用HA的配置
			
zk3 zk4 zk5 配置hadoop的HA大概可以分为以下几步: 配置zookpeer(namenode之间的通信要靠zk来实现) 配置hadoop的 hadoop-env.sh hdfs-sit ...
 - js文件操作之——导出Excel (js-xlsx)
			
前阵子跟server同学讨论一个Excel导出的需求,我说JS搞不定,需要server来做,被server同学强行打脸. 今天研究了下,尼玛,不光可以,还很强大了! 总结:经验是害人的,尤其是在发展迅 ...