Java 调用 shell 脚本详解
这一年的项目中,有大量的场景需要Java 进程调用 Linux的bash shell 脚本实现相关功能。
从之前的项目中拷贝的相关模块和网上的例子来看,有个别的“陷阱”造成调用shell 脚本在某些特殊的场景下,有一些奇奇怪怪的bug。
大家且听我一一道来。
先看看网上搜索到的例子:
- 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
- #!/bin/bash
- echo `pwd`
输出结果为:
- 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脚本的子进程卡死,这两种输出需要分别读取,而且不能互相影响。所以必须新开两个线程来进行读取。
我开始的实现如下:
- 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;
- }
- }
- 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卡死的情况。
- #!/bin/bash
- while true;do
- a=1
- sleep 0.1
- done
- 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;
- }
- }
- 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;
- }
- }
- 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()描述,这个方法是非阻塞的。
- 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方法有特别的注意事项。
- String command = "/home/Lance/workspace/someTest/testbash.sh 'hello world'";
- process = Runtime.getRuntime().exec(command);
等价于
- 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 脚本详解的更多相关文章
- 阿里语音识别(语音转文字)java调用全程手把手详解-适合中小学生快速上手
阿里语音识别服务java调用全程手把手详解-适合中小学生快速上手 阿里语音识别与百度语音识别的调用对比: 用例:1分30秒的录音文件 百度用时:3秒 阿里用时:30秒 识别准确率来看 ...
- java调用shell脚本小demo
复制指定文件cpp.sh: [root@localhost soft]# vim cpp.sh#!/bin/bash name="$1"\cp /home/soft/test/${ ...
- [转载]JAVA调用Shell脚本
FROM:http://blog.csdn.net/jj12345jj198999/article/details/11891701 在实际项目中,JAVA有时候需要调用C写出来的东西,除了JNI以外 ...
- java调用shell脚本,并获得结果集的例子
/** * 运行shell脚本 * @param shell 需要运行的shell脚本 */ public static void execShell(String shell){ try { Run ...
- java调用shell脚本
/** * 运行shell脚本 * @param shell 需要运行的shell脚本 */ public static void execShell(String shell){ try { Run ...
- JAVA调用shell脚本利用ansible修改多节点上的redis参数
创建hosts文件 创建ansible-playbook执行时所用到的hosts文件,例如 /etc/redis/hosts 利用shell命令根据传入的host名和地址写入hosts文件: #set ...
- java调用shell脚本执行操作
//定时清空 日志 String shellString = "sh /home/jyapp/delete_log.sh"; Process process = Runtime.g ...
- Java 执行Shell脚本指令
一.介绍 有时候我们在Linux中运行Java程序时,需要调用一些Shell命令和脚本.而Runtime.getRuntime().exec()方法给我们提供了这个功能,而且Runtime.getRu ...
- 用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql
1:创建shell脚本 touch sqoop_options.sh chmod 777 sqoop_options.sh 编辑文件 特地将执行map的个数设置为变量 测试 可以java代码传参数 ...
随机推荐
- dubbo支持哪些通信协议和序列化协议
dubbo支持的通信协议 dubbo协议 dubbo://192.168.0.1:20188 默认就是走dubbo协议的,单一长连接,NIO异步通信,基于hessian作为序列化协议 适用的场景就是: ...
- Swap file ".hive-site.xml.swp" already exists
1.使用命令[hadoop@slaver1 conf]$ ls -la查找出隐藏文件,然后删除报出来的错误文件. [hadoop@slaver1 conf]$ rm -rf .hive-site.xm ...
- 使用 curses 函数库管理基于文本的屏幕
curses 函数库提供了终端无关的方式来编写全屏幕的基于字符的程序.curses 还可以管理键盘,提供了一种简单易用的非阻塞字符输入模式. curses 函数库能够优化光标的移动并最小化需要对屏幕进 ...
- WebAPI——自动生成帮助文档
Web Api 自动生成帮助文档 新建Web Api项目之后,会在首页有API的导航菜单,点击即可看到API帮助文档,不过很遗憾,Description 是没有内容的. 怎么办呢? 第一步: 如果 ...
- MaterialEditText——Android Material Design EditText控件
MaterialEditText是Android Material Design EditText控件.可以定制浮动标签.主要颜色.默认的错误颜色等. 随着 Material Design 的到来, ...
- 【Android】修改Android 模拟器IMEI
修改Android 模拟器IMEI 在.....\android_sdk\tools文件下找到emulator-arm.exe,使用UltraEdit文本编辑器打开,搜索CGSN关键字,把000000 ...
- sparkStreaming消费kafka-1.0.1方式:direct方式(存储offset到zookeeper)
版本声明: kafka:1.0.1 spark:2.1.0 注意:在使用过程中可能会出现servlet版本不兼容的问题,因此在导入maven的pom文件的时候,需要做适当的排除操作 <?xml ...
- python编码,赋值和is的区别
1. == 与 is 的区别 赋值 == 比较值是否相等,is 比较,比较的是内存地址. 小数据池的作用是节省内存空间 数字的范围:-5 到 256 共用一个数据池 字符串范围:1.不能有特殊字符.2 ...
- js,JQuery 生成二维码
代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- BZOJ4025 二分图 分治 并查集 二分图 带权并查集按秩合并
原文链接http://www.cnblogs.com/zhouzhendong/p/8683831.html 题目传送门 - BZOJ4025 题意 有$n$个点,有$m$条边.有$T$个时间段.其中 ...