java执行Linux命令

public static boolean execCurl(String upstream , String domain) throws Exception {
        //参数判断-这里不写了
        String[] cmds = {"curl", "-d", upstream, Cons.NGINX_HTTP_URL + domain};
       // String[] cmdA = { "/bin/sh", "-c", cmd };//如果有管道 echo |  ps -ef|grep 之类的
        ProcessBuilder pb = new ProcessBuilder(cmds);
        pb.redirectErrorStream(true);
        Process p;
        BufferedReader br = null;
        String line = null;
        StringBuffer sb = new StringBuffer();
        try {
            p = pb.start();
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            if (sb.toString().contains("success")) {
                return true;
            }
        } catch (IOException e) {
            throw e;
        } finally {
            if (br != null) {
                br.close();
            }
        }
        return false;
    }

你可能感兴趣的:(源码)