java執(zhí)行shell命令及日志收集避坑指南分享
有時(shí)候我們需要調(diào)用系統(tǒng)命令執(zhí)行一些東西,可能是為了方便,也可能是沒有辦法必須要調(diào)用。涉及執(zhí)行系統(tǒng)命令的東西,則就不能做跨平臺(tái)了,這和java語言的初衷是相背的。
廢話不多說,java如何執(zhí)行shell命令?自然是調(diào)用java語言類庫提供的接口API了。
1. java執(zhí)行shell的api
執(zhí)行shell命令,可以說系統(tǒng)級(jí)的調(diào)用,編程語言自然必定會(huì)提供相應(yīng)api操作了。
在java中,有兩個(gè)api供調(diào)用:Runtime.exec(), Process API. 簡(jiǎn)單使用如下:
1.1. Runtime.exec() 實(shí)現(xiàn)
調(diào)用實(shí)現(xiàn)如下:
import java.io.InputStream;
public class RuntimeExecTest {
@Test
public static void testRuntimeExec() {
try {
Process process = Runtime.getRuntime()
.exec("cmd.exe /c dir");
process.waitFor();
}
catch (Exception e) {
e.printStackTrace();
}
}
}簡(jiǎn)單的說就是只有一行調(diào)用即可:Runtime.getRuntime().exec("cmd.exe /c dir") ; 看起來非常簡(jiǎn)潔。
1.2. ProcessBuilder 實(shí)現(xiàn)
使用ProcessBuilder需要自己操作更多東西,也因此可以自主設(shè)置更多東西。
(但實(shí)際上底層與Runtime是一樣的了),用例如下:
public class ProcessBuilderTest {
@Test
public void testProcessBuilder() {
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("ipconfig");
//將標(biāo)準(zhǔn)輸入流和錯(cuò)誤輸入流合并,通過標(biāo)準(zhǔn)輸入流讀取信息
processBuilder.redirectErrorStream(true);
try {
//啟動(dòng)進(jìn)程
Process start = processBuilder.start();
//獲取輸入流
InputStream inputStream = start.getInputStream();
//轉(zhuǎn)成字符輸入流
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "gbk");
int len = -1;
char[] c = new char[1024];
StringBuffer outputString = new StringBuffer();
//讀取進(jìn)程輸入流中的內(nèi)容
while ((len = inputStreamReader.read(c)) != -1) {
String s = new String(c, 0, len);
outputString.append(s);
System.out.print(s);
}
inputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}看起來是要麻煩些,但實(shí)際上是差不多的,只是上一個(gè)用例沒有處理輸出日志而已。
但總體來說的 ProcessBuilder 的可控性更強(qiáng),所以一般使用這個(gè)會(huì)更自由些。
以下Runtime.exec()的實(shí)現(xiàn):
// java.lang.Runtime#exec
public Process exec(String[] cmdarray, String[] envp, File dir)
throws IOException {
// 僅為 ProcessBuilder 的一個(gè)封裝
return new ProcessBuilder(cmdarray)
.environment(envp)
.directory(dir)
.start();
}2. 調(diào)用shell思考事項(xiàng)
從上面來看,要調(diào)用系統(tǒng)命令,并非難事。那是否就意味著我們可以隨便調(diào)用現(xiàn)成方案進(jìn)行處理工作呢?當(dāng)然不是,我們應(yīng)當(dāng)要考慮幾個(gè)問題?
1. 調(diào)用系統(tǒng)命令是進(jìn)程級(jí)別的調(diào)用;
- 進(jìn)程與線程的差別大家懂的,更加重量級(jí),開銷更大。
- 在java中,我們更多的是使用多線程進(jìn)行并發(fā)。
- 但如果用于系統(tǒng)調(diào)用,那就是進(jìn)程級(jí)并發(fā)了,而且外部進(jìn)程不再受jvm控制,出了問題也就不好玩了。
- 所以,不要隨便調(diào)用系統(tǒng)命令是個(gè)不錯(cuò)的實(shí)踐。
2. 調(diào)用系統(tǒng)命令是硬件相關(guān)的調(diào)用;
- java語言的思想是一次編寫,到處使用。
- 但如果你使用的系統(tǒng)調(diào)用,則不好處理了,因?yàn)槊總€(gè)系統(tǒng)支持的命令并非完全一樣的,你的代碼也就會(huì)因環(huán)境的不一樣而表現(xiàn)不一致了。
- 健壯性就下來了,所以,少用為好。
3. 內(nèi)存是否夠用?
- 一般我們jvm作為一個(gè)獨(dú)立進(jìn)程運(yùn)行,會(huì)被分配足夠多的內(nèi)存,以保證運(yùn)行的順暢與高效。
- 這時(shí),可能留給系統(tǒng)的空間就不會(huì)太多了,而此時(shí)再調(diào)用系統(tǒng)進(jìn)程運(yùn)行業(yè)務(wù),則得提前預(yù)估下咯。
4. 進(jìn)程何時(shí)停止?
- 當(dāng)我調(diào)起一個(gè)系統(tǒng)進(jìn)程之后,我們后續(xù)如何操作?
- 比如是異步調(diào)用的話,可能就忽略掉結(jié)果了。
- 而如果是同步調(diào)用的話,則當(dāng)前線程必須等待進(jìn)程退出,這樣會(huì)讓我們的業(yè)務(wù)大大簡(jiǎn)單化了。
- 因?yàn)楫惒叫枰紤]的事情往往很多。
5. 如何獲取進(jìn)程日志信息?
- 一個(gè)shell進(jìn)程的調(diào)用,可能是一個(gè)比較耗時(shí)的操作,此時(shí)應(yīng)該是只要任何進(jìn)度,就應(yīng)該匯報(bào)出來,從而避免外部看起來一直沒有響應(yīng),從而無法判定是死掉了還是在運(yùn)行中。
- 而外部進(jìn)程的通信,又不像一個(gè)普通io的調(diào)用,直接輸出結(jié)果信息。
- 這往往需要我們通過兩個(gè)輸出流進(jìn)行捕獲。而如何讀取這兩個(gè)輸出流數(shù)據(jù),就成了我們獲取日志信息的關(guān)鍵了。
- ProcessBuilder 是使用inputStream 和 errStream 來表示兩個(gè)輸出流, 分別對(duì)應(yīng)操作系統(tǒng)的標(biāo)準(zhǔn)輸出流和錯(cuò)誤輸出流。
- 但這兩個(gè)流都是阻塞io流,如果處理不當(dāng),則會(huì)引起系統(tǒng)假死的風(fēng)險(xiǎn)。
6. 進(jìn)程的異常如何捕獲?
- 在jvm線程里產(chǎn)生的異常,可以很方便的直接使用try...catch... 捕獲,而shell調(diào)用的異常呢?
- 它實(shí)際上并不能直接拋出異常,我們可以通過進(jìn)程的返回碼來判定是否發(fā)生了異常,這些錯(cuò)誤碼一般會(huì)遵循操作系統(tǒng)的錯(cuò)誤定義規(guī)范,但時(shí)如果是我們自己寫的shell或者其他同學(xué)寫的shell就無法保證了。
- 所以,往往除了我們要捕獲錯(cuò)誤之外,至少要規(guī)定0為正確的返回碼。
- 其他錯(cuò)誤碼也盡量不要亂用。
- 其次,我們還應(yīng)該在發(fā)生錯(cuò)誤時(shí),能從錯(cuò)誤輸出流信息中,獲取到些許的蛛絲馬跡,以便我們可以快速排錯(cuò)。
以上問題,如果都能處理得當(dāng),那么我認(rèn)為,這個(gè)調(diào)用就是安全的。反之則是有風(fēng)險(xiǎn)的。
不過,問題看著雖然多,但都是些細(xì)化的東西,也無需太在意。
基本上,我們通過線程池來控制進(jìn)程的膨脹問題;通過讀取io流來解決異常信息問題;通過調(diào)用類型規(guī)劃內(nèi)存及用量問題;
3. 完整的shell調(diào)用參考
說了這么多理論,還不如來點(diǎn)實(shí)際。
don't bb, show me the code!
import com.my.mvc.app.common.exception.ShellProcessExecException;
import com.my.mvc.app.common.helper.NamedThreadFactory;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.FileUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 功能描述: Shell命令運(yùn)行工具類封裝
*
*/
@Log4j2
public class ShellCommandExecUtil {
/**
* @see #runShellCommandSync(String, String[], Charset, String)
*/
public static int runShellCommandSync(String baseShellDir, String[] cmd,
Charset outputCharset) throws IOException {
return runShellCommandSync(baseShellDir, cmd, outputCharset, null);
}
/**
* 真正運(yùn)行shell命令
*
* @param baseShellDir 運(yùn)行命令所在目錄(先切換到該目錄后再運(yùn)行命令)
* @param cmd 命令數(shù)組
* @param outputCharset 日志輸出字符集,一般windows為GBK, linux為utf8
* @param logFilePath 日志輸出文件路徑, 為空則直接輸出到當(dāng)前應(yīng)用日志中,否則寫入該文件
* @return 進(jìn)程退出碼, 0: 成功, 其他:失敗
* @throws IOException 執(zhí)行異常時(shí)拋出
*/
public static int runShellCommandSync(String baseShellDir, String[] cmd,
Charset outputCharset, String logFilePath)
throws IOException {
long startTime = System.currentTimeMillis();
boolean needReadProcessOutLogStreamByHand = true;
log.info("【cli】receive new Command. baseDir: {}, cmd: {}, logFile:{}",
baseShellDir, String.join(" ", cmd), logFilePath);
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.directory(new File(baseShellDir));
initErrorLogHolder(logFilePath, outputCharset);
int exitCode = 0;
try {
if(logFilePath != null) {
ensureFilePathExists(logFilePath);
// String redirectLogInfoAndErrCmd = " > " + logFilePath + " 2>&1 ";
// cmd = mergeTwoArr(cmd, redirectLogInfoAndErrCmd.split("\\s+"));
pb.redirectErrorStream(true);
pb.redirectOutput(new File(logFilePath));
needReadProcessOutLogStreamByHand = false;
}
Process p = pb.start();
if(needReadProcessOutLogStreamByHand) {
readProcessOutLogStream(p, outputCharset);
}
try {
p.waitFor();
}
catch (InterruptedException e) {
log.error("進(jìn)程被中斷", e);
setProcessLastError("中斷異常:" + e.getMessage());
}
finally {
exitCode = p.exitValue();
log.info("【cli】process costTime:{}ms, exitCode:{}",
System.currentTimeMillis() - startTime, exitCode);
}
if(exitCode != 0) {
throw new ShellProcessExecException(exitCode,
"進(jìn)程返回異常信息, returnCode:" + exitCode
+ ", lastError:" + getProcessLastError());
}
return exitCode;
}
finally {
removeErrorLogHolder();
}
}
/**
* 使用 Runtime.exec() 運(yùn)行shell
*/
public static int runShellWithRuntime(String baseShellDir,
String[] cmd,
Charset outputCharset) throws IOException {
long startTime = System.currentTimeMillis();
initErrorLogHolder(null, outputCharset);
Process p = Runtime.getRuntime().exec(cmd, null, new File(baseShellDir));
readProcessOutLogStream(p, outputCharset);
int exitCode;
try {
p.waitFor();
}
catch (InterruptedException e) {
log.error("進(jìn)程被中斷", e);
setProcessLastError("中斷異常:" + e.getMessage());
}
catch (Throwable e) {
log.error("其他異常", e);
setProcessLastError(e.getMessage());
}
finally {
exitCode = p.exitValue();
log.info("【cli】process costTime:{}ms, exitCode:{}",
System.currentTimeMillis() - startTime, exitCode);
}
if(exitCode != 0) {
throw new ShellProcessExecException(exitCode,
"進(jìn)程返回異常信息, returnCode:" + exitCode
+ ", lastError:" + getProcessLastError());
}
return exitCode;
}
/**
* 確保文件夾存在
*
* @param filePath 文件路徑
* @throws IOException 創(chuàng)建文件夾異常拋出
*/
public static void ensureFilePathExists(String filePath) throws IOException {
File path = new File(filePath);
if(path.exists()) {
return;
}
File p = path.getParentFile();
if(p.mkdirs()) {
log.info("為文件創(chuàng)建目錄: {} 成功", p.getPath());
return;
}
log.warn("創(chuàng)建目錄:{} 失敗", p.getPath());
}
/**
* 合并兩個(gè)數(shù)組數(shù)據(jù)
*
* @param arrFirst 左邊數(shù)組
* @param arrAppend 要添加的數(shù)組
* @return 合并后的數(shù)組
*/
public static String[] mergeTwoArr(String[] arrFirst, String[] arrAppend) {
String[] merged = new String[arrFirst.length + arrAppend.length];
System.arraycopy(arrFirst, 0,
merged, 0, arrFirst.length);
System.arraycopy(arrAppend, 0,
merged, arrFirst.length, arrAppend.length);
return merged;
}
/**
* 刪除以某字符結(jié)尾的字符
*
* @param originalStr 原始字符
* @param toTrimChar 要檢測(cè)的字
* @return 裁剪后的字符串
*/
public static String trimEndsWith(String originalStr, char toTrimChar) {
char[] value = originalStr.toCharArray();
int i = value.length - 1;
while (i > 0 && value[i] == toTrimChar) {
i--;
}
return new String(value, 0, i + 1);
}
/**
* 錯(cuò)誤日志讀取線程池(不設(shè)上限)
*/
private static final ExecutorService errReadThreadPool = Executors.newCachedThreadPool(
new NamedThreadFactory("ReadProcessErrOut"));
/**
* 最后一次異常信息
*/
private static final Map<Thread, ProcessErrorLogDescriptor>
lastErrorHolder = new ConcurrentHashMap<>();
/**
* 主動(dòng)讀取進(jìn)程的標(biāo)準(zhǔn)輸出信息日志
*
* @param process 進(jìn)程實(shí)體
* @param outputCharset 日志字符集
* @throws IOException 讀取異常時(shí)拋出
*/
private static void readProcessOutLogStream(Process process,
Charset outputCharset) throws IOException {
try (BufferedReader stdInput = new BufferedReader(new InputStreamReader(
process.getInputStream(), outputCharset))) {
Thread parentThread = Thread.currentThread();
// 另起一個(gè)線程讀取錯(cuò)誤消息,必須先啟該線程
errReadThreadPool.submit(() -> {
try {
try (BufferedReader stdError = new BufferedReader(
new InputStreamReader(process.getErrorStream(), outputCharset))) {
String err;
while ((err = stdError.readLine()) != null) {
log.error("【cli】{}", err);
setProcessLastError(parentThread, err);
}
}
}
catch (IOException e) {
log.error("讀取進(jìn)程錯(cuò)誤日志輸出時(shí)發(fā)生了異常", e);
setProcessLastError(parentThread, e.getMessage());
}
});
// 外部線程讀取標(biāo)準(zhǔn)輸出消息
String stdOut;
while ((stdOut = stdInput.readLine()) != null) {
log.info("【cli】{}", stdOut);
}
}
}
/**
* 新建一個(gè)進(jìn)程錯(cuò)誤信息容器
*
* @param logFilePath 日志文件路徑,如無則為 null
*/
private static void initErrorLogHolder(String logFilePath, Charset outputCharset) {
lastErrorHolder.put(Thread.currentThread(),
new ProcessErrorLogDescriptor(logFilePath, outputCharset));
}
/**
* 移除錯(cuò)誤日志監(jiān)聽
*/
private static void removeErrorLogHolder() {
lastErrorHolder.remove(Thread.currentThread());
}
/**
* 獲取進(jìn)程的最后錯(cuò)誤信息
*
* 注意: 該方法只會(huì)在父線程中調(diào)用
*/
private static String getProcessLastError() {
Thread thread = Thread.currentThread();
return lastErrorHolder.get(thread).getLastError();
}
/**
* 設(shè)置最后一個(gè)錯(cuò)誤信息描述
*
* 使用當(dāng)前線程或自定義
*/
private static void setProcessLastError(String lastError) {
lastErrorHolder.get(Thread.currentThread()).setLastError(lastError);
}
private static void setProcessLastError(Thread thread, String lastError) {
lastErrorHolder.get(thread).setLastError(lastError);
}
/**
* 判斷當(dāng)前系統(tǒng)是否是 windows
*/
public static boolean isWindowsSystemOs() {
return System.getProperty("os.name").toLowerCase()
.startsWith("win");
}
/**
* 進(jìn)程錯(cuò)誤信息描述封裝類
*/
private static class ProcessErrorLogDescriptor {
/**
* 錯(cuò)誤信息記錄文件
*/
private String logFile;
/**
* 最后一行錯(cuò)誤信息
*/
private String lastError;
private Charset charset;
ProcessErrorLogDescriptor(String logFile, Charset outputCharset) {
this.logFile = logFile;
charset = outputCharset;
}
String getLastError() {
if(lastError != null) {
return lastError;
}
try{
if(logFile == null) {
return null;
}
List<String> lines = FileUtils.readLines(
new File(logFile), charset);
StringBuilder sb = new StringBuilder();
for (int i = lines.size() - 1; i >= 0; i--) {
sb.insert(0, lines.get(i) + "\n");
if(sb.length() > 200) {
break;
}
}
return sb.toString();
}
catch (Exception e) {
log.error("【cli】讀取最后一次錯(cuò)誤信息失敗", e);
}
return null;
}
void setLastError(String err) {
if(lastError == null) {
lastError = err;
return;
}
lastError = lastError + "\n" + err;
if(lastError.length() > 200) {
lastError = lastError.substring(lastError.length() - 200);
}
}
}
}以上實(shí)現(xiàn),完成了我們?cè)诘?點(diǎn)中討論的幾個(gè)問題:
- 1. 主要使用 ProcessBuilder 完成了shell的調(diào)用;
- 2. 支持讀取進(jìn)程的所有輸出信息,且在必要的時(shí)候,支持使用單獨(dú)的文件進(jìn)行接收輸出日志;
- 3. 在進(jìn)程執(zhí)行異常時(shí),支持拋出對(duì)應(yīng)異常,且給出一定的errMessage描述;
- 4. 如果想控制調(diào)用進(jìn)程的數(shù)量,則在外部調(diào)用時(shí)控制即可;
- 5. 使用兩個(gè)線程接收兩個(gè)輸出流,避免出現(xiàn)應(yīng)用假死,使用newCachedThreadPool線程池避免過快創(chuàng)建線程;
接下來,我們進(jìn)行下單元測(cè)試:
public class ShellCommandExecUtilTest {
@Test
public void testRuntimeShell() throws IOException {
int errCode;
errCode = ShellCommandExecUtil.runShellWithRuntime("E:\\tmp",
new String[] {"cmd", "/c", "dir"}, Charset.forName("gbk"));
Assert.assertEquals("進(jìn)程返回碼不正確", 0, errCode);
}
@Test(expected = ShellProcessExecException.class)
public void testRuntimeShellWithErr() throws IOException {
int errCode;
errCode = ShellCommandExecUtil.runShellWithRuntime("E:\\tmp",
new String[] {"cmd", "/c", "dir2"}, Charset.forName("gbk"));
Assert.fail("dir2 應(yīng)該要執(zhí)行失敗,但卻通過了,請(qǐng)查找原因");
}
@Test
public void testProcessShell1() throws IOException {
int errCode;
errCode = ShellCommandExecUtil.runShellCommandSync("/tmp",
new String[]{"cmd", "/c", "dir"}, Charset.forName("gbk"));
Assert.assertEquals("進(jìn)程返回碼不正確", 0, errCode);
String logPath = "/tmp/cmd.log";
errCode = ShellCommandExecUtil.runShellCommandSync("/tmp",
new String[]{"cmd", "/c", "dir"}, Charset.forName("gbk"), logPath);
Assert.assertTrue("結(jié)果日志文件不存在", new File(logPath).exists());
}
@Test(expected = ShellProcessExecException.class)
public void testProcessShell1WithErr() throws IOException {
int errCode;
errCode = ShellCommandExecUtil.runShellCommandSync("/tmp",
new String[]{"cmd", "/c", "dir2"}, Charset.forName("gbk"));
Assert.fail("dir2 應(yīng)該要執(zhí)行失敗,但卻通過了,請(qǐng)查找原因");
}
@Test(expected = ShellProcessExecException.class)
public void testProcessShell1WithErr2() throws IOException {
int errCode;
String logPath = "/tmp/cmd2.log";
try {
errCode = ShellCommandExecUtil.runShellCommandSync("/tmp",
new String[]{"cmd", "/c", "dir2"}, Charset.forName("gbk"), logPath);
}
catch (ShellProcessExecException e) {
e.printStackTrace();
throw e;
}
Assert.assertTrue("結(jié)果日志文件不存在", new File(logPath).exists());
}
}至此,我們的一個(gè)安全可靠的shell運(yùn)行功能就搞定了。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java Vector和ArrayList的異同分析及實(shí)例講解
在本篇文章里小編給大家整理的是一篇關(guān)于Java Vector和ArrayList的異同分析及實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2021-01-01
SpringBoot Tomcat漏洞修復(fù)的解決方法
本文主要介紹了SpringBoot Tomcat漏洞修復(fù)的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04
MyBatis實(shí)現(xiàn)批量插入方法實(shí)例
最近在公司項(xiàng)目開發(fā)中遇到批量數(shù)據(jù)插入或者更新,下面這篇文章主要給大家介紹了關(guān)于MyBatis實(shí)現(xiàn)批量插入的相關(guān)資料,需要的朋友可以參考下2022-10-10
Spring?Boot中使用Spring?Retry重試框架的操作方法
這篇文章主要介紹了Spring?Retry?在SpringBoot?中的應(yīng)用,介紹了RetryTemplate配置的時(shí)候,需要設(shè)置的重試策略和退避策略,需要的朋友可以參考下2022-04-04

