JAVA 调用linux shell命令



package com.test.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.cnunisoft.util.TextLogger;

public class JavaShellUtil {

/**
* 调用方法为: JavaShellUtil javaShellUtil = new JavaShellUtil();
//参数为要执行的Shell命令,即通过调用Shell脚本sendKondorFile.sh将/temp目录下的tmp.pdf文件发送到192.168.1.200上
int success = JavaShellUtil.executeShell("sh /tmp/sendKondorFile.sh /temp tmp.pdf",false);
* @param shellCommand
* @return
* @throws IOException
*/
public static int executeShell(String shellCommand,boolean waitFor) throws IOException {
int success = 1;//默认是成功的
StringBuffer stringBuffer = new StringBuffer();
BufferedReader bufferedReader = null;
// 格式化日期时间,记录日志时使用
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS ");

try {
stringBuffer.append(dateFormat.format(new Date()))
.append("exec Shell:").append(shellCommand)
.append(" \r\n");

Process pid = null;
//String[] cmd = { "/bin/sh", "-c", shellCommand };
// 执行Shell命令
pid = Runtime.getRuntime().exec(shellCommand);
if (pid != null) {
stringBuffer.append("pid:").append(pid.toString())
.append("\r\n");
if(waitFor){
pid.waitFor();
}
bufferedReader = new BufferedReader(new InputStreamReader(
pid.getInputStream()), 1024);
} else {
stringBuffer.append("no pid\r\n");
}
stringBuffer.append(dateFormat.format(new Date())).append(
"Shell result:\r\n");
String line = null;
// 读取Shell的输出内容,并添加到stringBuffer中
while (bufferedReader != null
&& (line = bufferedReader.readLine()) != null) {
stringBuffer.append(line).append("\r\n");
}
} catch (Exception ioe) {
stringBuffer.append("Shell Exception:\r\n").append(ioe.getMessage())
.append("\r\n");
success = 0;
} finally {
if(bufferedReader!=null){
bufferedReader.close();
}
TextLogger.getLogger().warning(stringBuffer.toString());
}
return success;
}

/**
* 带返回值的shell调用
* 注意linux并不是所有的都有返回值的,要特殊处理ls是有的 ,但是ps -ef|grep就么有
* @param shellCommand
* @return
* @throws IOException
*/
public static String executeShellWidthReturn(String shellCommand){
//返回值
StringBuffer stringBufferReturn = new StringBuffer();
//记录日志
StringBuffer stringBufferLog = new StringBuffer();
// 格式化日期时间,记录日志时使用
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS ");
InputStreamReader ir = null;
try {
stringBufferLog.append(dateFormat.format(new Date()))
.append("Will Exec Shell:").append(shellCommand)
.append(" \r\n");

Process process = null;
//String[] cmd = { "/bin/sh", "-c", shellCommand };
// 执行Shell命令
process = Runtime.getRuntime().exec(shellCommand);
// ir用于读取Shell的输出内容
ir=new InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader (ir);
if (process == null){
stringBufferLog.append("Exec Error\r\n");
}
stringBufferLog.append(dateFormat.format(new Date())).append(
"Exec Completed \r\nResult:\r\n");
String line = null;
// 读取Shell的输出内容,并添加到stringBuffer中
while ((line = input.readLine ()) != null) {
stringBufferLog.append(line).append("\r\n");
stringBufferReturn.append(line);
}
} catch (Exception ioe) {
stringBufferLog.append("Exec Exception:\r\n").append(ioe.getMessage())
.append("\r\n");
} finally {
if(ir!=null){
try {
ir.close();
} catch (IOException e) {
}
}
TextLogger.getLogger().warning(stringBufferLog.toString());
}
return stringBufferReturn.toString();
}

/**
* 调用方法为: JavaShellUtil javaShellUtil = new JavaShellUtil();
//参数为要执行的Shell命令,即通过调用Shell脚本sendKondorFile.sh将/temp目录下的tmp.pdf文件发送到192.168.1.200上
int success = JavaShellUtil.executeShell("sh /tmp/sendKondorFile.sh /temp tmp.pdf",false);
* @param shellCommand
* @return
* @throws IOException
*/
public static int executeCommand(String shellCommand,boolean waitFor) throws IOException {
int success = 1;//默认是成功的
StringBuffer stringBuffer = new StringBuffer();
BufferedReader bufferedReader = null;
// 格式化日期时间,记录日志时使用
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS ");

try {
stringBuffer.append(dateFormat.format(new Date()))
.append("exec Shell:").append(shellCommand)
.append(" \r\n");

Process pid = null;
String[] cmd = { "/bin/sh", "-c", shellCommand };
// 执行Shell命令
pid = Runtime.getRuntime().exec(cmd);
if (pid != null) {
stringBuffer.append("pid:").append(pid.toString())
.append("\r\n");
if(waitFor){
pid.waitFor();
}
bufferedReader = new BufferedReader(new InputStreamReader(
pid.getInputStream()), 1024);
} else {
stringBuffer.append("no pid\r\n");
}
stringBuffer.append(dateFormat.format(new Date())).append(
"Shell result:\r\n");
String line = null;
// 读取Shell的输出内容,并添加到stringBuffer中
while (bufferedReader != null
&& (line = bufferedReader.readLine()) != null) {
stringBuffer.append(line).append("\r\n");
}
} catch (Exception ioe) {
stringBuffer.append("Shell Exception:\r\n").append(ioe.getMessage())
.append("\r\n");
success = 0;
} finally {
if(bufferedReader!=null){
bufferedReader.close();
}
TextLogger.getLogger().warning(stringBuffer.toString());
}
return success;
}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值