1. 程式人生 > >Java 調用 shell 腳本詳解

Java 調用 shell 腳本詳解

生產環境 inpu lai home throws next erl 當前 例子

這一年的項目中,有大量的場景需要Java 進程調用 Linux的bash shell 腳本實現相關功能。

從之前的項目中拷貝的相關模塊和網上的例子來看,有個別的“陷阱”造成調用shell 腳本在某些特殊的場景下,有一些奇奇怪怪的bug。

大家且聽我一一道來。

先看看網上搜索到的例子:

[java] view plain copy
  1. package someTest;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. public class ShellTest {
  6. public static void main(String[] args) {
  7. InputStreamReader stdISR = null;
  8. InputStreamReader errISR = null;
  9. Process process = null;
  10. String command = "/home/Lance/workspace/someTest/testbash.sh";
  11. try {
  12. process = Runtime.getRuntime().exec(command);
  13. int exitValue = process.waitFor();
  14. String line = null;
  15. stdISR = new InputStreamReader(process.getInputStream());
  16. BufferedReader stdBR = new BufferedReader(stdISR);
  17. while ((line = stdBR.readLine()) != null) {
  18. System.out.println("STD line:" + line);
  19. }
  20. errISR = new InputStreamReader(process.getErrorStream());
  21. BufferedReader errBR = new BufferedReader(errISR);
  22. while ((line = errBR.readLine()) != null) {
  23. System.out.println("ERR line:" + line);
  24. }
  25. } catch (IOException | InterruptedException e) {
  26. e.printStackTrace();
  27. } finally {
  28. try {
  29. if (stdISR != null) {
  30. stdISR.close();
  31. }
  32. if (errISR != null) {
  33. errISR.close();
  34. }
  35. if (process != null) {
  36. process.destroy();
  37. }
  38. } catch (IOException e) {
  39. System.out.println("正式執行命令:" + command + "有IO異常");
  40. }
  41. }
  42. }
  43. }

testbash.sh

[plain] view plain copy
  1. #!/bin/bash
  2. echo `pwd`

輸出結果為:

[plain] view plain copy
  1. 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
  1. package someTest;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.util.LinkedList;
  7. import java.util.List;
  8. public class CommandStreamGobbler extends Thread {
  9. private InputStream is;
  10. private String command;
  11. private String prefix = "";
  12. private boolean readFinish = false;
  13. private boolean ready = false;
  14. private List<String> infoList = new LinkedList<String>();
  15. CommandStreamGobbler(InputStream is, String command, String prefix) {
  16. this.is = is;
  17. this.command = command;
  18. this.prefix = prefix;
  19. }
  20. public void run() {
  21. InputStreamReader isr = null;
  22. try {
  23. isr = new InputStreamReader(is);
  24. BufferedReader br = new BufferedReader(isr);
  25. String line = null;
  26. ready = true;
  27. while ((line = br.readLine()) != null) {
  28. infoList.add(line);
  29. System.out.println(prefix + " line: " + line);
  30. }
  31. } catch (IOException ioe) {
  32. System.out.println("正式執行命令:" + command + "有IO異常");
  33. } finally {
  34. try {
  35. if (isr != null) {
  36. isr.close();
  37. }
  38. } catch (IOException ioe) {
  39. System.out.println("正式執行命令:" + command + "有IO異常");
  40. }
  41. readFinish = true;
  42. }
  43. }
  44. public InputStream getIs() {
  45. return is;
  46. }
  47. public String getCommand() {
  48. return command;
  49. }
  50. public boolean isReadFinish() {
  51. return readFinish;
  52. }
  53. public boolean isReady() {
  54. return ready;
  55. }
  56. public List<String> getInfoList() {
  57. return infoList;
  58. }
  59. }

[java] view plain copy
  1. package someTest;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. public class ShellTest {
  5. public static void main(String[] args) {
  6. InputStreamReader stdISR = null;
  7. InputStreamReader errISR = null;
  8. Process process = null;
  9. String command = "/home/Lance/workspace/someTest/testbash.sh";
  10. try {
  11. process = Runtime.getRuntime().exec(command);
  12. CommandStreamGobbler errorGobbler = new CommandStreamGobbler(process.getErrorStream(), command, "ERR");
  13. CommandStreamGobbler outputGobbler = new CommandStreamGobbler(process.getInputStream(), command, "STD");
  14. errorGobbler.start();
  15. // 必須先等待錯誤輸出ready再建立標準輸出
  16. while (!errorGobbler.isReady()) {
  17. Thread.sleep(10);
  18. }
  19. outputGobbler.start();
  20. while (!outputGobbler.isReady()) {
  21. Thread.sleep(10);
  22. }
  23. int exitValue = process.waitFor();
  24. } catch (IOException | InterruptedException e) {
  25. e.printStackTrace();
  26. } finally {
  27. try {
  28. if (stdISR != null) {
  29. stdISR.close();
  30. }
  31. if (errISR != null) {
  32. errISR.close();
  33. }
  34. if (process != null) {
  35. process.destroy();
  36. }
  37. } catch (IOException e) {
  38. System.out.println("正式執行命令:" + command + "有IO異常");
  39. }
  40. }
  41. }
  42. }


到此為止,解決了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
  1. #!/bin/bash
  2. while true;do
  3. a=1
  4. sleep 0.1
  5. done

[java] view plain copy
  1. package someTest;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.util.LinkedList;
  7. import java.util.List;
  8. public class CommandStreamGobbler extends Thread {
  9. private InputStream is;
  10. private String command;
  11. private String prefix = "";
  12. private boolean readFinish = false;
  13. private boolean ready = false;
  14. // 命令執行結果,0:執行中 1:超時 2:執行完成
  15. private int commandResult = 0;
  16. private List<String> infoList = new LinkedList<String>();
  17. CommandStreamGobbler(InputStream is, String command, String prefix) {
  18. this.is = is;
  19. this.command = command;
  20. this.prefix = prefix;
  21. }
  22. public void run() {
  23. InputStreamReader isr = null;
  24. BufferedReader br = null;
  25. try {
  26. isr = new InputStreamReader(is);
  27. br = new BufferedReader(isr);
  28. String line = null;
  29. ready = true;
  30. while (commandResult != 1) {
  31. if (br.ready() || commandResult == 2) {
  32. if ((line = br.readLine()) != null) {
  33. infoList.add(line);
  34. } else {
  35. break;
  36. }
  37. } else {
  38. Thread.sleep(100);
  39. }
  40. }
  41. } catch (IOException | InterruptedException ioe) {
  42. System.out.println("正式執行命令:" + command + "有IO異常");
  43. } finally {
  44. try {
  45. if (br != null) {
  46. br.close();
  47. }
  48. if (isr != null) {
  49. isr.close();
  50. }
  51. } catch (IOException ioe) {
  52. System.out.println("正式執行命令:" + command + "有IO異常");
  53. }
  54. readFinish = true;
  55. }
  56. }
  57. public InputStream getIs() {
  58. return is;
  59. }
  60. public String getCommand() {
  61. return command;
  62. }
  63. public boolean isReadFinish() {
  64. return readFinish;
  65. }
  66. public boolean isReady() {
  67. return ready;
  68. }
  69. public List<String> getInfoList() {
  70. return infoList;
  71. }
  72. public void setTimeout(int timeout) {
  73. this.commandResult = timeout;
  74. }
  75. }

[java] view plain copy
  1. package someTest;
  2. public class CommandWaitForThread extends Thread {
  3. private Process process;
  4. private boolean finish = false;
  5. private int exitValue = -1;
  6. public CommandWaitForThread(Process process) {
  7. this.process = process;
  8. }
  9. public void run() {
  10. try {
  11. this.exitValue = process.waitFor();
  12. } catch (InterruptedException e) {
  13. e.printStackTrace();
  14. } finally {
  15. finish = true;
  16. }
  17. }
  18. public boolean isFinish() {
  19. return finish;
  20. }
  21. public void setFinish(boolean finish) {
  22. this.finish = finish;
  23. }
  24. public int getExitValue() {
  25. return exitValue;
  26. }
  27. }

[java] view plain copy
  1. package someTest;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Date;
  5. public class ShellTest {
  6. public static void main(String[] args) {
  7. InputStreamReader stdISR = null;
  8. InputStreamReader errISR = null;
  9. Process process = null;
  10. String command = "/home/Lance/workspace/someTest/testbash.sh";
  11. long timeout = 10 * 1000;
  12. try {
  13. process = Runtime.getRuntime().exec(command);
  14. CommandStreamGobbler errorGobbler = new CommandStreamGobbler(process.getErrorStream(), command, "ERR");
  15. CommandStreamGobbler outputGobbler = new CommandStreamGobbler(process.getInputStream(), command, "STD");
  16. errorGobbler.start();
  17. // 必須先等待錯誤輸出ready再建立標準輸出
  18. while (!errorGobbler.isReady()) {
  19. Thread.sleep(10);
  20. }
  21. outputGobbler.start();
  22. while (!outputGobbler.isReady()) {
  23. Thread.sleep(10);
  24. }
  25. CommandWaitForThread commandThread = new CommandWaitForThread(process);
  26. commandThread.start();
  27. long commandTime = new Date().getTime();
  28. long nowTime = new Date().getTime();
  29. boolean timeoutFlag = false;
  30. while (!commandIsFinish(commandThread, errorGobbler, outputGobbler)) {
  31. if (nowTime - commandTime > timeout) {
  32. timeoutFlag = true;
  33. break;
  34. } else {
  35. Thread.sleep(100);
  36. nowTime = new Date().getTime();
  37. }
  38. }
  39. if (timeoutFlag) {
  40. // 命令超時
  41. errorGobbler.setTimeout(1);
  42. outputGobbler.setTimeout(1);
  43. System.out.println("正式執行命令:" + command + "超時");
  44. }else {
  45. // 命令執行完成
  46. errorGobbler.setTimeout(2);
  47. outputGobbler.setTimeout(2);
  48. }
  49. while (true) {
  50. if (errorGobbler.isReadFinish() && outputGobbler.isReadFinish()) {
  51. break;
  52. }
  53. Thread.sleep(10);
  54. }
  55. } catch (IOException | InterruptedException e) {
  56. e.printStackTrace();
  57. } finally {
  58. if (process != null) {
  59. process.destroy();
  60. }
  61. }
  62. }
  63. private boolean commandIsFinish(CommandWaitForThread commandThread, CommandStreamGobbler errorGobbler, CommandStreamGobbler outputGobbler) {
  64. if (commandThread != null) {
  65. return commandThread.isFinish();
  66. } else {
  67. return (errorGobbler.isReadFinish() && outputGobbler.isReadFinish());
  68. }
  69. }
  70. }

在以上的代碼中,為了防止線程被阻塞,要點如下:

1. 在CommandStreamGobbler裏,bufferedReader在readLine()之前,先用ready()看一下當前緩沖區的情況,請特別註意ready()描述,這個方法是非阻塞的。

[java] view plain copy
  1. boolean java.io.BufferedReader.ready() throws IOException
  2. 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.
  3. Returns:
  4. 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
  1. String command = "/home/Lance/workspace/someTest/testbash.sh ‘hello world‘";
  2. process = Runtime.getRuntime().exec(command);


等價於

[java] view plain copy
  1. List<String> commandList = new LinkedList<String>();
  2. commandList.add("/home/Lance/workspace/someTest/testbash.sh");
  3. commandList.add("hello world");
  4. String[] commands = new String[commandList.size()];
  5. for (int i = 0; i < commandList.size(); i++) {
  6. commands[i] = commandList.get(i);
  7. }
  8. process = Runtime.getRuntime().exec(commands);

好了,今天介紹到這裏。

Java 調用 shell 腳本詳解