linux上java调用shell脚本

// java调用shell脚本

executeShellData(edepShellPath, businessDate);

方法一:使用Runtime.getRuntime().exec()

/**
* 方法简述:java执行shell脚本
* <p>
* 方法详细描述:使用Runtime.getRuntime().exec()

* @Since: 2016-04-11
* @param execPath
* @param businessDate
* @return
*/
private void executeShellData(String execPath, String businessDate) {
LOGGER.info("executeShellData贴源报表统计执行shell脚本开始!execPath[{}],businessDate[{}]",execPath, businessDate);
InputStreamReader isr = null;
String result = null;
try {
Process proc = Runtime.getRuntime().exec(
"sh " + execPath + " " + businessDate);
BufferedReader br = null;
StringBuffer sb = new StringBuffer();
try {
isr = new InputStreamReader(proc.getInputStream(),
Constant.CORE_FILE_CHARACTERSET_GBK);
br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
LOGGER.info("executeShellData br.readLine()=[{}]", line);
sb.append(line).append(Constant.UNIX_LINE_FEED);
}
result = sb.toString();
} catch (Exception e) {
LOGGER.error(
"executeShellData贴源报表统计执行shell脚本获取输入流失败!result[{}]",
result, e);
} finally {
if (br != null) {
br.close();
}
if (isr != null) {
isr.close();
}
}
proc.destroy();
} catch (IOException e) {
LOGGER.error("executeShellData贴源报表统计执行shell脚本,启动进程失败!", e);
} catch (Exception e) {
LOGGER.error("executeShellData贴源报表统计,执行shell脚本失败!result[{}]",
result, e);
}
LOGGER.info(
"executeShellDataEnd贴源报表统计,java调用shell脚本结束!edepShellPath[{}], result[{}]",
execPath, result);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值