阅读背景:

java调用linux中的shell脚本并返回执行结果

来源:互联网 

 

 

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;
 
public 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();
        }
    }
}package cn.com.songjy.test.shell;
 
impo




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: