Java 調用 shell 腳本詳解
這一年的項目中,有大量的場景需要Java 進程調用 Linux的bash shell 腳本實現相關功能。
從之前的項目中拷貝的相關模塊和網上的例子來看,有個別的“陷阱”造成調用shell 腳本在某些特殊的場景下,有一些奇奇怪怪的bug。
大家且聽我一一道來。
先看看網上搜索到的例子:
[java] view plain copy
- package someTest;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class ShellTest {
- public static void main(String[] args) {
- InputStreamReader stdISR = null;
- InputStreamReader errISR = null;
- Process process = null;
- String command = "/home/Lance/workspace/someTest/testbash.sh";
- try {
- process = Runtime.getRuntime().exec(command);
- int exitValue = process.waitFor();
- String line = null;
- stdISR = new InputStreamReader(process.getInputStream());
- BufferedReader stdBR = new BufferedReader(stdISR);
- while ((line = stdBR.readLine()) != null) {
- System.out.println("STD line:" + line);
- }
- errISR = new InputStreamReader(process.getErrorStream());
- BufferedReader errBR = new BufferedReader(errISR);
- while ((line = errBR.readLine()) != null) {
- System.out.println("ERR line:" + line);
- }
- } catch (IOException | InterruptedException e) {
- e.printStackTrace();
- } finally {
- try {
- if (stdISR != null) {
- stdISR.close();
- }
- if (errISR != null) {
- errISR.close();
- }
- if (process != null) {
- process.destroy();
- }
- } catch (IOException e) {
- System.out.println("正式執行命令:" + command + "有IO異常");
- }
- }
- }
- }
testbash.sh
[plain] view plain copy
- #!/bin/bash
- echo `pwd`
輸出結果為:
[plain] view plain copy
- STD line:/home/Lance/workspace/someTest
Java在執行Runtime.getRuntime().exec(command)之後,Linux會創建一個進程,該進程與JVM進程建立三個管道連接,標準輸入流、標準輸出流、標準錯誤流。
上述代碼,依次讀取標準輸出流和標準錯誤流,在shell給出“退出信號”後,做了相應的清理工作。
對於一般場景來說,這段代碼可以湊合用了。但是,在實際場景中,會有以下幾個“陷阱”。
一. 當標準輸出流或標準錯誤流非常龐大的時候,會出現調用waitFor方法卡死的bug。
真實的環境中,當標準輸出在10000行左右的時候,就會出現卡死的情況。
原因分析:假設linux進程不斷向標準輸出流和標準錯誤流寫數據,而JVM卻不讀取,數據會暫存在linux緩存區,當緩存區存滿之後導致該進程無法繼續寫數據,會僵死,導致java進程會卡死在waitFor()處,永遠無法結束。
解決方式:由於標準輸出和錯誤輸出都會向Linux緩存區寫數據,而腳本如何輸出這兩種流是Java端不能確定的。為了不讓shell腳本的子進程卡死,這兩種輸出需要分別讀取,而且不能互相影響。所以必須新開兩個線程來進行讀取。
我開始的實現如下:
[java] view plain copy
- package someTest;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.LinkedList;
- import java.util.List;
- public class CommandStreamGobbler extends Thread {
- private InputStream is;
- private String command;
- private String prefix = "";
- private boolean readFinish = false;
- private boolean ready = false;
- private List<String> infoList = new LinkedList<String>();
- CommandStreamGobbler(InputStream is, String command, String prefix) {
- this.is = is;
- this.command = command;
- this.prefix = prefix;
- }
- public void run() {
- InputStreamReader isr = null;
- try {
- isr = new InputStreamReader(is);
- BufferedReader br = new BufferedReader(isr);
- String line = null;
- ready = true;
- while ((line = br.readLine()) != null) {
- infoList.add(line);
- System.out.println(prefix + " line: " + line);
- }
- } catch (IOException ioe) {
- System.out.println("正式執行命令:" + command + "有IO異常");
- } finally {
- try {
- if (isr != null) {
- isr.close();
- }
- } catch (IOException ioe) {
- System.out.println("正式執行命令:" + command + "有IO異常");
- }
- readFinish = true;
- }
- }
- public InputStream getIs() {
- return is;
- }
- public String getCommand() {
- return command;
- }
- public boolean isReadFinish() {
- return readFinish;
- }
- public boolean isReady() {
- return ready;
- }
- public List<String> getInfoList() {
- return infoList;
- }
- }
[java] view plain copy
- package someTest;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class ShellTest {
- public static void main(String[] args) {
- InputStreamReader stdISR = null;
- InputStreamReader errISR = null;
- Process process = null;
- String command = "/home/Lance/workspace/someTest/testbash.sh";
- 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);
- }
- int exitValue = process.waitFor();
- } catch (IOException | InterruptedException e) {
- e.printStackTrace();
- } finally {
- try {
- if (stdISR != null) {
- stdISR.close();
- }
- if (errISR != null) {
- errISR.close();
- }
- if (process != null) {
- process.destroy();
- }
- } catch (IOException e) {
- System.out.println("正式執行命令:" + command + "有IO異常");
- }
- }
- }
- }
到此為止,解決了Java卡死shell腳本的情況。再說說,第二種可能。
二. 由於shell腳本的編寫問題,當其自身出現僵死的情況,上述代碼出現Java代碼被僵死的Shell腳本阻塞住的情況。
原因分析:由於shell腳本也是人寫的,難免會出現失誤。在Java調用shell腳本時,無論是Debug場景還是生產環境,都發生過shell腳本意外僵死反過來卡死Java相關線程的情況。典型的表現為:shell腳本長時間運行,標準輸出和錯誤輸出沒有任何輸出(包括結束符),操作系統顯示shell腳本在正常運行或僵死,沒有退出信號。
解決方式:上述代碼中,至少有三處會導致線程阻塞,包括標準輸出和錯誤輸出這線程的BufferedReader的readline方法,以及Process的waitFor方法。解決這個問題的核心有兩個,1.避免任何Java線程被阻塞住,因為一旦被IO阻塞住,線程將處於內核態,主線程沒有任何辦法強制結束相關子線程。2.添加一個簡單的超時機制,超時後回收相應的線程資源,並結束調用過程。
演示代碼中,我改寫了testshell.sh,寫一個沒有任何輸出的死循環模擬shell卡死的情況。
[plain] view plain copy
- #!/bin/bash
- while true;do
- a=1
- sleep 0.1
- done
[java] view plain copy
- package someTest;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.LinkedList;
- import java.util.List;
- public class CommandStreamGobbler extends Thread {
- private InputStream is;
- private String command;
- private String prefix = "";
- private boolean readFinish = false;
- private boolean ready = false;
- // 命令執行結果,0:執行中 1:超時 2:執行完成
- private int commandResult = 0;
- private List<String> infoList = new LinkedList<String>();
- CommandStreamGobbler(InputStream is, String command, String prefix) {
- this.is = is;
- this.command = command;
- this.prefix = prefix;
- }
- public void run() {
- InputStreamReader isr = null;
- BufferedReader br = null;
- try {
- isr = new InputStreamReader(is);
- br = new BufferedReader(isr);
- String line = null;
- ready = true;
- while (commandResult != 1) {
- if (br.ready() || commandResult == 2) {
- if ((line = br.readLine()) != null) {
- infoList.add(line);
- } else {
- break;
- }
- } else {
- Thread.sleep(100);
- }
- }
- } catch (IOException | InterruptedException ioe) {
- System.out.println("正式執行命令:" + command + "有IO異常");
- } finally {
- try {
- if (br != null) {
- br.close();
- }
- if (isr != null) {
- isr.close();
- }
- } catch (IOException ioe) {
- System.out.println("正式執行命令:" + command + "有IO異常");
- }
- readFinish = true;
- }
- }
- public InputStream getIs() {
- return is;
- }
- public String getCommand() {
- return command;
- }
- public boolean isReadFinish() {
- return readFinish;
- }
- public boolean isReady() {
- return ready;
- }
- public List<String> getInfoList() {
- return infoList;
- }
- public void setTimeout(int timeout) {
- this.commandResult = timeout;
- }
- }
[java] view plain copy
- package someTest;
- 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;
- }
- }
[java] view plain copy
- package someTest;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Date;
- public class ShellTest {
- public static void main(String[] args) {
- 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 (!commandIsFinish(commandThread, errorGobbler, outputGobbler)) {
- if (nowTime - commandTime > timeout) {
- timeoutFlag = true;
- break;
- } else {
- Thread.sleep(100);
- nowTime = new Date().getTime();
- }
- }
- if (timeoutFlag) {
- // 命令超時
- errorGobbler.setTimeout(1);
- outputGobbler.setTimeout(1);
- System.out.println("正式執行命令:" + command + "超時");
- }else {
- // 命令執行完成
- errorGobbler.setTimeout(2);
- outputGobbler.setTimeout(2);
- }
- while (true) {
- if (errorGobbler.isReadFinish() && outputGobbler.isReadFinish()) {
- break;
- }
- Thread.sleep(10);
- }
- } catch (IOException | InterruptedException e) {
- e.printStackTrace();
- } finally {
- if (process != null) {
- process.destroy();
- }
- }
- }
- private boolean commandIsFinish(CommandWaitForThread commandThread, CommandStreamGobbler errorGobbler, CommandStreamGobbler outputGobbler) {
- if (commandThread != null) {
- return commandThread.isFinish();
- } else {
- return (errorGobbler.isReadFinish() && outputGobbler.isReadFinish());
- }
- }
- }
在以上的代碼中,為了防止線程被阻塞,要點如下:
1. 在CommandStreamGobbler裏,bufferedReader在readLine()之前,先用ready()看一下當前緩沖區的情況,請特別註意ready()描述,這個方法是非阻塞的。
[java] view plain copy
- boolean java.io.BufferedReader.ready() throws IOException
- Tells whether this stream is ready to be read. A buffered character stream is ready if the buffer is not empty, or if the underlying character stream is ready.
- Returns:
- True if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.
2.在一個新線程commandThread中,調用process對象的waitFor()從而避免主線程卡死,主線程的最後會執行finally塊中的process.destory()保證commandThread正常退出。
以上的兩點改進,保證了Java在調用shell腳本過程互不被對方卡死的機制。
三.在執行shell腳本過程中,可能會添加參數,通常在終端中,我們使用“ ”(空格)把參數隔開。
為了區分空格是作為參數分隔符,還是參數的一部分。調用exec方法有特別的註意事項。
[java] view plain copy
- String command = "/home/Lance/workspace/someTest/testbash.sh ‘hello world‘";
- process = Runtime.getRuntime().exec(command);
等價於
[java] view plain copy
- List<String> commandList = new LinkedList<String>();
- commandList.add("/home/Lance/workspace/someTest/testbash.sh");
- commandList.add("hello world");
- String[] commands = new String[commandList.size()];
- for (int i = 0; i < commandList.size(); i++) {
- commands[i] = commandList.get(i);
- }
- process = Runtime.getRuntime().exec(commands);
好了,今天介紹到這裏。
Java 調用 shell 腳本詳解