以往一直都是crontab+shell调用java程序,最近需要反过来,使用java调用shell程序,实现定时管理,今天总结一下。
基础内容:
java的java.lang.Runtime类提供了exec静态方法,可以执行本地脚本
程序事例:
package study; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class JavaShellUtil { // 基本路径 private static final String basePath = "/tmp/"; // 记录Shell执行状况的日志文件的位置(绝对路径) private static final String executeShellLogFile = basePath + "executeShell.log"; public static int executeShell(String shellCommand) throws IOException { int success = 0; 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("准备执行Shell命令 ").append(shellCommand).append(" "); Process pid = null; String[] cmd = { "/bin/sh", "-c", shellCommand }; // 执行Shell命令 pid = Runtime.getRuntime().exec(cmd); if (pid != null) { stringBuffer.append("进程号:").append(pid.toString()).append(" "); // bufferedReader用于读取Shell的输出内容 bufferedReader = new BufferedReader(new InputStreamReader(pid.getInputStream()), 1024); int return = pid.waitFor(); //返回值是执行的结果,如果不是0,就是错误 } else { stringBuffer.append("没有pid "); } stringBuffer.append(dateFormat.format(new Date())).append("Shell命令执行完毕 执行结果为: "); String line = null; // 读取Shell的输出内容,并添加到stringBuffer中 while (bufferedReader != null && (line = bufferedReader.readLine()) != null) { stringBuffer.append(line).append(" "); } } catch (Exception ioe) { stringBuffer.append("执行Shell命令时发生异常: ").append(ioe.getMessage()).append(" "); } if (bufferedReader != null) { OutputStreamWriter outputStreamWriter = null; try { bufferedReader.close(); // 将Shell的执行情况输出到日志文件中 OutputStream outputStream = new FileOutputStream(executeShellLogFile); outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8"); outputStreamWriter.write(stringBuffer.toString()); } catch (Exception e) { e.printStackTrace(); } finally { outputStreamWriter.close(); } success = 1; } return success; } public static void main(String args[]) { try { int i = JavaShellUtil.executeShell("ls /"); System.out.println(i); } catch (IOException e) { // TODO Auto-generated catch block System.out.println(e); } } }
环境说明:
1、日志文件:/tmp/executeShell.log
2、将JavaShellUtil.java放置到study目录,然后执行:
- javac study/JavaShellUtil.java
- java study/JavaShellUtil
捕获异常
java希望捕获shell的异常,除了上面的return,还可以使用Process对象的exitValue()方法,例如:pid.exitValue()。
当然如果希望获取shell的异常输出,那么还需要优化一段代码:
if (pid != null) { stringBuffer.append("进程号:").append(pid.toString()).append(" "); // bufferedReader用于读取Shell的输出内容 bufferedReader = new BufferedReader(new InputStreamReader(pid.getInputStream()), 1024); InputStreamReader stderr = new InputStreamReader(pid.getErrorStream()); success = pid.waitFor(); if (success!=0){ BufferedReader br = new BufferedReader(stderr); while ( (line = br.readLine()) != null){ logger.error(String.format("[%s]:[%s]:[%s] execute shell error [%s]", this.getJobName(), this.getTaskName(), this.getTaskRecordId(), line)); return success; } }