package com.house365.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Date: 2017/2/8</p>
 *
 * @author XN
 * @version 1.0
 */
public class JavaShellUtil {

    // 基本路径
    private static final String basePath = "/home/";

    public static int executeShell(String shellCommand) throws IOException {
        System.out.println("shellCommand:" + shellCommand);
        int success = 0;
        StringBuffer stringBuffer = new StringBuffer();
        BufferedReader bufferedReader = null;
        BufferedReader stdError = 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()));
                //读到标准出错的信息
                stdError = new BufferedReader(new InputStreamReader(pid.getErrorStream()));
                //这个是或得脚本执行的返回值
                int status = pid.waitFor();


                //如果脚本执行的返回值不是0,则表示脚本执行失败，否则（值为0）脚本执行成功。
                if (status != 0) {
                    stringBuffer.append("shell脚本执行失败！");
                } else {
                    stringBuffer.append("shell脚本执行成功！");
                }
            } else {
                stringBuffer.append("没有pid\r\n");
            }
            stringBuffer.append(dateFormat.format(new Date())).append("Shell命令执行完毕\r\n执行结果为：\r\n");

            //将标准输入流上面的内容写到stringBuffer里面
            String line = null;
            // 读取Shell的输出内容，并添加到stringBuffer中
            while (bufferedReader != null && (line = bufferedReader.readLine()) != null) {
                stringBuffer.append(line).append("\r\n");
            }

            //将标准输入流上面的内容写到stringBuffer里面
            String line1 = null;
            while (stdError != null && (line1 = stdError.readLine()) != null) {
                stringBuffer.append(line1).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) {
                System.out.println("stringBuffer.toString():" + stringBuffer.toString());
            }
            success = 1;
        }
        return success;
    }

}
