关于java调用linux shell 的问题
问题的提出:
- shell脚本要做离线的数据处理任务
- java调用脚本,将这种处理任务封装成webservice
特点:
- shell处理单个时间长
- 每次要处理文件量大
这里目前只做调用分析:
原来的:
private void runShell(String cmd){
try{ logger.info("the command "+cmd);
// create a process for the shell
ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);
pb.redirectErrorStream(true); // use this to capture messages sent to stderr
Process shell = pb.start();
InputStream shellIn = shell.getInputStream(); // this captures the output from the command
int shellExitStatus = 0;
try {
shellExitStatus = shell.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
} // wait for the shell to finish and get the return code // // at this point you can process the output issued by the command
// // for instance, this reads the output and writes it to System.out:
int c;
while ((c = shellIn.read()) != -1) {
logger.info("shell read value:"+c);
}
// close the stream
shellIn.close();
logger.info(" *** End *** "+shellExitStatus); logger.info("pb command "+pb.command());
logger.info("pb command dir "+pb.directory());
logger.info(pb.environment());
logger.info(System.getenv());
logger.info(System.getProperties());
}
catch (IOException ignoreMe)
{
ignoreMe.printStackTrace();
}
}
修改之后的代码:
//变为成员变量方式
protected volatile Process process; private void runShell(String cmd) {
try {
logger.info("the command " + cmd);
// create a process for the shell
ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);
process = pb.start();
final InputStream inputStream = process.getInputStream();
final InputStream errorStream = process.getErrorStream();
new Thread(new Runnable() {
@Override
public void run() {
try{
BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
String line;
while((line=reader.readLine())!=null){
// logConsole(line);
logger.debug(line);
}
}catch(Exception e){
// log(e);
e.printStackTrace();
logger.debug("接收日志出错,推出日志接收");
}
}
},"one").start();
new Thread(new Runnable() {
@Override
public void run() {
try {
BufferedReader reader=new BufferedReader(new InputStreamReader(errorStream));
String line;
while((line=reader.readLine())!=null){
// logConsole(line);
logger.debug(line);
}
} catch (Exception e) {
// log(e);
e.printStackTrace();
logger.debug("接收日志出错,推出日志接收");
}
}
},"error").start();
// InputStream shellIn = process.getInputStream(); // this captures the output from the command
int shellExitStatus = 0;
try {
//等待程序结果
shellExitStatus = process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
process=null;
}
} catch (IOException ignoreMe) {
ignoreMe.printStackTrace();
}
}
实验结果:
同时处理100条语音。
第一个用时50min。
第二个用时25min。
优势:
- 使用processbuild 构造基于os的进程
- 使用process作为类变量并且使用volatile来进行描述
关于java调用linux shell 的问题的更多相关文章
- java 调用bash shell脚本阻塞的小问题的解决
java 调用bash shell脚本阻塞的小问题的解决 背景 使用java实现的web端,web端相应用户的界面操作,使用java调用bash实现的shell脚本进行实际的操作,操作完成返回执行结 ...
- Java调用Linux命令(cd的处理)
一.Java调用Linux系统的命令非常简单 这是一个非常常用的调用方法示例: public String executeLinuxCmd(String cmd) { System.out.print ...
- Java调用Linux命令执行
调用方式 Java调用linux命令执行的方式有两种,一种是直接调用linux命令,一种是将linux命令写到.sh脚本中,然后调用脚本执行. 详细说明 直接调用:使用java中lang包下面的Run ...
- java调用Linux执行Python爬虫,并将数据存储到elasticsearch--(环境脚本搭建)
java调用Linux执行Python爬虫,并将数据存储到elasticsearch中 一.以下博客代码使用的开发工具及环境如下: 1.idea: 2.jdk:1.8 3.elasticsearch: ...
- java调用linux下的so库
1.编写java类 public class Abc { static { System.loadLibrary("abc"); } public native static St ...
- java调用Linux命令报错:java.io.IOException: Cannot run program "ps": CreateProcess error=2, ?????????
在idea里面,java代码:Runtime.getRuntime().exec("ps -aux") 是因为默认是用windows平台运行了,所以报错,得改成调用Linux平台运 ...
- python 调用Linux shell
有时候难免需要直接调用Shell命令来完成一些比较简单的操作,比如mount一个文件系统之类的.那么我们使用Python如何调用Linux的Shell命令?下面来介绍几种常用的方法: 1. os 模块 ...
- Java调用Linux下的shell命令并将结果以流的形式返回
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader; public cl ...
- python 之调用Linux shell命令及相关高级应用
最近根据老大要求,将数据进行同步备份,结合第三方提供的工具.第三方服务其实是有python demo的,本想研究下实际的python sdk搞个demo开发的,但是发现有些组建装起来确实头大,而且本公 ...
随机推荐
- delphi函数调用约定
指令 参数存放位置 参数传递顺序 参数内存管理 使用地方 Register CPU寄存器 从左到右 被调用者 默认,published属性存取方法必须使用 Pascal 栈 从左到右 被调用者 向后兼 ...
- DevExpress 表中数据导出
gridView1.ExportToXlsx("SampleStock.xlsx"); if (true) { DevExpress.XtraEditors.XtraMessage ...
- SQLServer2005,2000获取表结构:字段名、类型、长度、主键、非空、注释
SQLServer 2005 SELECT d.name N'TableName', d.xtype N'TableType', a.colorder N'ColumnIndex', a.name N ...
- 任务管理界面添加显示RAM信息
显示RAM信息的核心代码是大蛋的,我只不过是整理下教程而已! 大蛋应该不会介意的吧,首先你需要apktool和SystemUI.apk,framework-res.apk 然后开始加载框架和反编译.. ...
- ios中怎么样调节占位文字与字体大小在同一高度
在设置好字体以后,在占位文字中设置leading这个字体属性,用leading来乘以一个比例(CGFloat)来调节位置.
- Enyim.Caching 客户端配置及示例
一.工作准备 memcached客户端:Enyim.Caching.2.13 memcached服务器:memcached-win64-1.4.4-14 备注:不建议使用windows服务器,开发环境 ...
- [转]Linux下的图形库介绍
[转]Linux 下的图形库介绍 http://blog.csdn.net/gogor/article/details/5925925 在进行Linux下的图形系统编程时,我们常常会遇到以下这些概念: ...
- Content Template & DataTemplate 区别
转一篇很好的博客: http://www.cnblogs.com/lzhp/p/3250786.html 介绍 listbox 的 Template.ItemsPanel.ItemContainerS ...
- python pandas/numpy
import pandas as pdpd.merge(dataframe1,dataframe2,on='common_field',how='outer') replace NaN datafra ...
- Quartus II Error总结与解答
(1).Error (209015): Can't configure device. Expected JTAG ID code 0x020B20DD for device 1, but found ...