zoukankan      html  css  js  c++  java
  • ubuntu java启动shell脚本

        public void startC138() {
            new Thread(() -> {
                try {
                    Thread.sleep(10000);
                    String killredis = "/home/bjlthy/gym/code/killredis.sh";
                    Process ps = Runtime.getRuntime().exec(killredis);
                    String logMsg = "----------> java killredis ps.isAlive=" + ps.isAlive();
                    System.out.println(logMsg);
                    LogHelper.info(logMsg);
    
                    Thread.sleep(5000);
    
                    startC2();
    
                } catch (Exception e) {
                    e.printStackTrace();
                    LogHelper.error("startC138", e);
                }
            }).start();
        }
    
        public void startC2() {
            ProcessBuilder pb;
            Process process = null;
            BufferedReader br = null;
            StringBuilder resMsg = null;
            OutputStream os = null;
            String cmd1 = "/home/bjlthy/gym/code/redis.sh";
            try {
                pb = new ProcessBuilder(cmd1);
                pb.redirectErrorStream(true);
                process = pb.start();
                os = process.getOutputStream();
                os.write(cmd1.getBytes());
                os.flush();
                os.close();
    
                resMsg = new StringBuilder();
    
                br = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String s;
                while ((s = br.readLine()) != null) {
                    resMsg.append(s + "
    ");
                }
                resMsg.deleteCharAt(resMsg.length() - 1);
                int result = process.waitFor();
                System.out.println("result=" + result);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (os != null) {
                        os.close();
                    }
                    if (br != null) {
                        br.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                // if (process != null) {
                //     process.destroy();
                // }
            }
            // return resMsg.toString();
        }
    
  • 相关阅读:
    Python Turtle
    Python 键盘记录
    Django框架学习
    MongoDB数据库安装与连接
    Python 进程间通信
    Powershell脚本执行权限
    Python 端口,IP扫描
    Exchange超级实用命令行
    Exchange管理界面
    window7 配置node.js 和coffeescript环境
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/14932102.html
Copyright © 2011-2022 走看看