java调用linux中的shell脚本
来源:互联网 发布:如何去做淘宝客 编辑:程序博客网 时间:2024/05/16 06:02
原文
package cn.com.songjy.test.shell; import java.io.BufferedReader;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date; //http://kongcodecenter.iteye.com/blog/1231177//参考http://siye1982.iteye.com/blog/592405//参考http://blog.csdn.net/christophe2008/article/details/6046456public class JavaShellUtil { // 基本路径 private static final String basePath = "/root/"; // 记录Shell执行状况的日志文件的位置(绝对路径) private static final String executeShellLogFile = basePath + "executeShell.log"; // 发送文件到Kondor系统的Shell的文件名(绝对路径) private static final String sendKondorShellName = basePath + "songjy.sh"; public int executeShell(String shellCommand) throws IOException { System.out.println("shellCommand:"+shellCommand); 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(" \r\n"); Process pid = null; String[] cmd = { "/bin/sh", "-c", shellCommand }; // 执行Shell命令 pid = Runtime.getRuntime().exec(cmd); if (pid != null) { stringBuffer.append("进程号:").append(pid.toString()) .append("\r\n"); // bufferedReader用于读取Shell的输出内容 bufferedReader = new BufferedReader(new InputStreamReader(pid.getInputStream()), 1024); pid.waitFor(); } else { stringBuffer.append("没有pid\r\n"); } stringBuffer.append(dateFormat.format(new Date())).append( "Shell命令执行完毕\r\n执行结果为:\r\n"); String line = null; // 读取Shell的输出内容,并添加到stringBuffer中 while (bufferedReader != null && (line = bufferedReader.readLine()) != null) { stringBuffer.append(line).append("\r\n"); } System.out.println("stringBuffer:"+stringBuffer); } catch (Exception ioe) { stringBuffer.append("执行Shell命令时发生异常:\r\n").append(ioe.getMessage()) .append("\r\n"); } finally { 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()); System.out.println("stringBuffer.toString():"+stringBuffer.toString()); } catch (Exception e) { e.printStackTrace(); } finally { outputStreamWriter.close(); } } success = 1; } return success; } public static void main(String[] args) { try { new JavaShellUtil().executeShell(sendKondorShellName); } catch (IOException e) { e.printStackTrace(); } }}
0 0
- java调用linux中的shell脚本
- java 调用linux shell脚本
- java调用linux中的shell脚本并返回执行结果
- linux中java调用shell脚本
- linux上java调用shell脚本
- Java调用执行linux shell脚本
- java调用Linux 命令、Shell脚本
- java调用Shell脚本
- java调用Shell脚本
- JAVA 调用shell脚本
- Java 调用Shell脚本
- shell脚本调用java
- Java调用shell脚本
- JAVA调用Shell脚本
- java调用Shell脚本
- java调用shell脚本
- Java调用Shell脚本
- Java调用Shell脚本
- 弹性云主机和vps的区别
- 回到顶部(滚动效果)js代码
- 内核:Linux2.6内核启动流程学习
- java中几个json工具分析测试
- jpg如何转换pdf格式
- java调用linux中的shell脚本
- android中子线程和主线程通信
- Android RecyclerView
- Linux下vim中文乱码的解决方法
- 使用hadoop实现ip地理位置统计~ip归属地和运营商
- 好看的儿童剧,孩子的动画王国——丑小鸭儿童剧团
- 影响Java EE性能的十大问题
- 中国余数定理
- Perl——正则表达式(一)字符匹配