关于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开发的,但是发现有些组建装起来确实头大,而且本公 ...
随机推荐
- 【转】Messagedlg
) = mrYes then Close; MessageDlg用法 对话框类型:mtwarning——含有感叹号的警告对话框mterror——含有红色叉符号的错误对话框mtinformation ...
- NuGet 的使用
install-package entityframework//Enable-Migrations -ContextTypeName College.Models.CollegeEntities ...
- 小白学jquery Mobile《构建跨平台APP:jQuery Mobile移动应用实战》连载四(场景切换)
作为一款真正有使用价值的应用,首先应该至少有两个页面,通过页面的切换来实现更多的交互.比如手机人人网,打开以后先是进入登录页面,登录后会有新鲜事,然后拉开左边的面板,能看到相册.悄悄话.应用之类的其他 ...
- jquery mobile最棘手的一个问题
大多数jquery mobile开发的妹子们都碰到过这个问题: 如何调用loading效果 这里给出一段代码,赶紧练手吧. //显示loading function showLoading(){ ...
- 【Django】Apache上运行单个Django项目,mod_wsgi配置
1 安装环境 操作系统:Ubuntu 12.04 LTS 32 位(安装在VMware虚拟机中) python 版本: Python 2.7.3 Django版本 >>> djang ...
- Python学习教程(learning Python)--3.3.4 Python的if-elif-else语句
Python的if-elif-else语句用于多种条件判断后选择某个语句块执行.该语句可以利用一系列条件表达式进行检查,并在某个表达式为真的情况下执行相应的代码.需要注意的是,虽然if/elif/el ...
- 自学Python三 Python中的屠龙刀(续)
装饰器: 在函数代码功能运行期间动态增加功能的方式叫做装饰器(Decorator).它对一个函数或者类进行再加工. 我们先定义两个函数,一个计算两数和,一个计算两数差. >>> de ...
- 第一个android应用程序
首先打开Eclipse和一个AVD.在Eclipse中选择File→New→Project→Android→Android Application Project 点击Next,按照下图所示填写 注: ...
- 别跟我来这套 Hot Swap 热插拔
如果你觉得我们演的不好,可以随时打断,如果你觉得怎么演好可以随时来改,你都可以直接来演.
- MEF(Managed Extensibility Framework) 微软平台插件化开发
体验Managed Extensibility Framework精妙的设计 MEF(Managed Extensibility Framework)是.NET Framework 4.0一个重要 ...