当前位置: 首页 > 工具软件 > jail-shell > 使用案例 >

JAVA 调用linux shell命令

咸正平
2023-12-01


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;
}
}


 类似资料: