java调用shell命令

  1. import java.io.BufferedReader;   
  2. import java.io.File;   
  3. import java.io.FileOutputStream;   
  4. import java.io.IOException;   
  5. import java.io.InputStreamReader;   
  6. import java.io.OutputStream;   
  7. import java.io.OutputStreamWriter;   
  8. import java.text.DateFormat;   
  9. import java.text.SimpleDateFormat;   
  10. import java.util.Date;   
  11.   
  12. public class JavaShellUtil {   
  13. //基本路径   
  14. private static final String basePath = "/tmp/";   
  15.   
  16. //记录Shell执行状况的日志文件的位置(绝对路径)   
  17. private static final String executeShellLogFile = basePath + "executeShell.log";   
  18.   
  19. //发送文件到Kondor系统的Shell的文件名(绝对路径)   
  20. private static final String sendKondorShellName = basePath + "sendKondorFile.sh";   
  21.   
  22. public int executeShell(String shellCommand) throws IOException {   
  23. int success = 0;   
  24. StringBuffer stringBuffer = new StringBuffer();   
  25. BufferedReader bufferedReader = null;   
  26. //格式化日期时间,记录日志时使用   
  27. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS ");   
  28.   
  29. try {   
  30. stringBuffer.append(dateFormat.format(new Date())).append("准备执行Shell命令 ").append(shellCommand).append(" \r\n");   
  31.   
  32. Process pid = null;   
  33. String[] cmd = {"/bin/sh""-c", shellCommand};   
  34. //执行Shell命令   
  35. pid = Runtime.getRuntime().exec(cmd);   
  36. if (pid != null) {   
  37. stringBuffer.append("进程号:").append(pid.toString()).append("\r\n");   
  38. //bufferedReader用于读取Shell的输出内容 bufferedReader = new BufferedReader(new InputStreamReader(pid.getInputStream()), 1024);   
  39. pid.waitFor();   
  40. else {   
  41. stringBuffer.append("没有pid\r\n");   
  42. }   
  43. stringBuffer.append(dateFormat.format(new Date())).append("Shell命令执行完毕\r\n执行结果为:\r\n");   
  44. String line = null;   
  45. //读取Shell的输出内容,并添加到stringBuffer中   
  46. while (bufferedReader != null &   
  47. &   
  48. (line = bufferedReader.readLine()) != null) {   
  49. stringBuffer.append(line).append("\r\n");   
  50. }   
  51. catch (Exception ioe) {   
  52. stringBuffer.append("执行Shell命令时发生异常:\r\n").append(ioe.getMessage()).append("\r\n");   
  53. finally {   
  54. if (bufferedReader != null) {   
  55. OutputStreamWriter outputStreamWriter = null;   
  56. try {   
  57. bufferedReader.close();   
  58. //将Shell的执行情况输出到日志文件中   
  59. OutputStream outputStream = new FileOutputStream(executeShellLogFile);   
  60. outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");   
  61. outputStreamWriter.write(stringBuffer.toString());   
  62. catch (Exception e) {   
  63. e.printStackTrace();   
  64. finally {   
  65. outputStreamWriter.close();   
  66. }   
  67. }   
  68. success = 1;   
  69. }   
  70. return success;   
  71. }   
  72.   
  73. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值