这一年的项目中,有大量的场景需要Java 进程调用 Linux的bash shell 脚本实现相关功能。

从之前的项目中拷贝的相关模块和网上的例子来看,有个别的“陷阱”造成调用shell 脚本在某些特殊的场景下,有一些奇奇怪怪的bug。

大家且听我一一道来。

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

  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

  1. #!/bin/bash
  2. echo `pwd`

输出结果为:

  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脚本的子进程卡死,这两种输出需要分别读取,而且不能互相影响。所以必须新开两个线程来进行读取。

我开始的实现如下:

  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. }
  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卡死的情况。

  1. #!/bin/bash
  2. while true;do
  3. a=1
  4. sleep 0.1
  5. done
  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. }
  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. }
  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()描述,这个方法是非阻塞的。

  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方法有特别的注意事项。

  1. String command = "/home/Lance/workspace/someTest/testbash.sh 'hello world'";
  2. process = Runtime.getRuntime().exec(command);

等价于

  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 脚本详解的更多相关文章

  1. 阿里语音识别(语音转文字)java调用全程手把手详解-适合中小学生快速上手

    阿里语音识别服务java调用全程手把手详解-适合中小学生快速上手 阿里语音识别与百度语音识别的调用对比: 用例:1分30秒的录音文件    百度用时:3秒    阿里用时:30秒    识别准确率来看 ...

  2. java调用shell脚本小demo

    复制指定文件cpp.sh: [root@localhost soft]# vim cpp.sh#!/bin/bash name="$1"\cp /home/soft/test/${ ...

  3. [转载]JAVA调用Shell脚本

    FROM:http://blog.csdn.net/jj12345jj198999/article/details/11891701 在实际项目中,JAVA有时候需要调用C写出来的东西,除了JNI以外 ...

  4. java调用shell脚本,并获得结果集的例子

    /** * 运行shell脚本 * @param shell 需要运行的shell脚本 */ public static void execShell(String shell){ try { Run ...

  5. java调用shell脚本

    /** * 运行shell脚本 * @param shell 需要运行的shell脚本 */ public static void execShell(String shell){ try { Run ...

  6. JAVA调用shell脚本利用ansible修改多节点上的redis参数

    创建hosts文件 创建ansible-playbook执行时所用到的hosts文件,例如 /etc/redis/hosts 利用shell命令根据传入的host名和地址写入hosts文件: #set ...

  7. java调用shell脚本执行操作

    //定时清空 日志 String shellString = "sh /home/jyapp/delete_log.sh"; Process process = Runtime.g ...

  8. Java 执行Shell脚本指令

    一.介绍 有时候我们在Linux中运行Java程序时,需要调用一些Shell命令和脚本.而Runtime.getRuntime().exec()方法给我们提供了这个功能,而且Runtime.getRu ...

  9. 用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql

    1:创建shell脚本 touch sqoop_options.sh chmod 777 sqoop_options.sh 编辑文件  特地将执行map的个数设置为变量  测试 可以java代码传参数 ...

随机推荐

  1. innobackupex做MySQL增量备份及恢复

    http://www.cnblogs.com/paul8339/p/6731688.html

  2. [转] 梦里Babel知多少(一)

    平时开发中,经常需要用到ES6/ES7的语法.那么就需要用到Babel来对代码进行转码处理. 之前用Vue比较多,所以以Vue-cli作为参考来分析.  第一张图是几个月前的Vue-cli生成的 第二 ...

  3. FORM表单中onclick()、submit()与onsubmit()的问题

    最近遇到一次处理form数据的过滤,采用了button的onclick事件来检查,发现return false后表单仍然提交了. 于是仔细研究了下onclick.onsubmit.submit集合函数 ...

  4. JAVA-ORM框架整理➣Mybatis操作MySQL

    概述 在Java中,对数据库操作的框架很多,上节概述Hibernate的简单使用,这里简单整理Mybatis的使用.Mybatis也是简单的数据库操作框架,通过IOC方式,获取操作类对象,进行数据的操 ...

  5. mysql inner jion多表查询

    select vtiger_users.id, vtiger_users.user_name, vtiger_role.rolename FROM vtiger_users inner join vt ...

  6. 高性能之js

    alloyteam团队的总结: 链接在:http://www.alloyteam.com/2012/10/high-performance-front-end-high-performance-jav ...

  7. 咸鱼入门到放弃1--JDBC

    JDBC参考微博https://www.cnblogs.com/surfcater/p/10224502.html 主要内容 1.JDBC相关概念 2.JDBC常用接口 driver connecti ...

  8. BZOJ1208 [HNOI2004]宠物收养所 splay

    原文链接http://www.cnblogs.com/zhouzhendong/p/8085803.html 题目传送门 - BZOJ1208 题意概括 有两种数,依次加入. 规则为下: 如果当前剩余 ...

  9. ActiveMQ在windows下启动失败解决方案

    activemq.xml文件中的 <transportConnectors> <!-- DOS protection, limit concurrent connections to ...

  10. MYSQL总览

    第一,二,三范式解决的是非主属性的关系.BC 范式解决的是主属性的关系:第二范式:就是完全依赖,没有部分依赖([id,b]->c 即不能c即依赖id又依赖b):[非主属性不能依赖于主键的一部分, ...