关于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开发的,但是发现有些组建装起来确实头大,而且本公 ...
随机推荐
- 一个封装好的CSV文件操作C#类代码
using System.Data; using System.IO; namespace DotNet.Utilities { /// <summary> /// CSV文件转换类 // ...
- 判断Check复选框是否选中
<div id="prm_div" style="font-size: 12px;" align="left"> <for ...
- DB2行转列(多维度)
多维度下进行行列转换,下面的行列转换时根据客户,所属银行机构进行的行列转换. -----------------建表 CREATE TABLE CUST_BANK_INFO ( CUST_ID ), ...
- linux rm 命令
1.命令格式: rm [选项] 文件… 2.命令功能: 删除一个目录中的一个或多个文件或目录,如果没有使用- r选项,则rm不会删除目录.如果使用 rm 来删除文件,通常仍可以将该文件恢复原状. 3. ...
- hdu 1277 全文检索
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1277 全文检索 Description 我们大家经常用google检索信息,但是检索信息的程序是很困难 ...
- ios中用drawRect方法绘图的时候设置颜色
设置画笔颜色可以直接 [[UIColor grayColor] set];就可以设置颜色.
- yum代理设置
vi /etc/yum.conf 加入以下:proxy=http://代理服务器ip:port 如果代理需要账号密码:proxy_username=userproxy_password=密码
- C++11 常用语法
1 新类型 C++ 11新增了long long和unsigned long long,以支持64bit宽度: 新增char16_t和char32_t以支持16位和32位字符表示: 增加了“原始”字符 ...
- layer 弹出子页面然后给父页面赋值
//----赋值 并关闭当前页面 开始---- FunctionActionDeleteXZ = function (CompanyId, RelCompanyName) { parent.$(&qu ...
- Quartus13.0破解方法
一定要按照步骤顺序才能破解,这里很关键 1.下载和打开Quartus II破解器,选择“应用”,选择“是”,找到bin(64位系统是bin64)目录下的sys_cpt.dll,“打开” 2.然后将li ...