package com.chinacreator.process.util;

/**
 * @author zhengrong.yan
 * @date 2021/2/2 23:14
 */

import java.io.*;

/**
 * @author ：dongbl
 * @version ：
 * @Description：
 * @date ：9:19 2017/11/14
 */
public class Bash {
    public static void cdncache() throws Exception {
        BufferedReader reader = null;

        String ip = "172.18.0.14";
        String bashCommand = "sh " + "/kc/newyearvideo/publish.sh" + " ffmpeg " + ip;
//            String bashCommand = "chmod 777 " + "/usr/local/java/jdk1.8.0_121/lib/stopffmpeg.sh" ;
//            String bashCommand = "kill -9" + ip;
        System.out.println(bashCommand);
        Runtime runtime = Runtime.getRuntime();
        Process pro = runtime.exec(bashCommand);
        int status = pro.waitFor();
        if (status != 0) {
            System.out.println("Failed to call shell's command ");
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(pro.getInputStream()));
        StringBuffer strbr = new StringBuffer();
        String line;
        while ((line = br.readLine()) != null) {
            strbr.append(line).append("\n");
        }

        String result = strbr.toString();
        System.out.println(result);
    }

    public static String exec(String command) throws Exception {
        String returnString = "";
        Process pro = null;
        Runtime runTime = Runtime.getRuntime();
        if (runTime == null) {
            System.err.println("Create runtime false!");
        }
        pro = runTime.exec(command);
        BufferedReader input = new BufferedReader(new InputStreamReader(pro.getInputStream()));
        PrintWriter output = new PrintWriter(new OutputStreamWriter(pro.getOutputStream()));
        String line;
        while ((line = input.readLine()) != null) {
            returnString = returnString + line + "\n";
        }
        input.close();
        output.close();
        pro.destroy();

        return returnString;
    }
}
