zoukankan      html  css  js  c++  java
  • java代码开启关闭线程(nginx)

    源码:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class demo3 {
        /**
         * @desc 启动进程 10.158 root root
         */
        public static void startProc() {
            System.out.println("开启进程:" + "nginx.exe");
    
            try {
                executeCmd("start nginx");
                executeCmd("start nginx");
            } catch (IOException e) {
                System.err.println("nginx.exe" + "线程开启失败");
                e.printStackTrace();
            }
        }
    
        /**
         * @desc 杀死进程
         */
        public static void killProc() {
            System.out.println("关闭进程:" + "nginx.exe");
            try {
                executeCmd("taskkill /F /IM " + "nginx.exe");
            } catch (IOException e) {
                e.printStackTrace();
                System.err.println("nginx.exe" + "线程关闭失败");
            }
        }
    
        /**
         * @desc 执行cmd命令
         */
        public static String executeCmd(String command) throws IOException {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec("cmd /c " + command);
            // Process process = runtime.exec( command);
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
            String line = null;
            StringBuilder build = new StringBuilder();
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                build.append(line);
            }
            return build.toString();
        }
    
        /**
         * @desc 执行cmd命令
         */
        public static String executeCmd2(String command) throws IOException {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec(command);
            // Process process = runtime.exec( command);
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
            String line = null;
            StringBuilder build = new StringBuilder();
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                build.append(line);
            }
            return build.toString();
        }
    
        /**
         * @desc 判断进程是否开启
         */
        public static boolean findProcess(String processName) {
            BufferedReader bufferedReader = null;
            try {
                Process proc = Runtime.getRuntime().exec("tasklist -fi " + '"' + "imagename eq " + processName + '"');
                bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                String line = null;
                while ((line = bufferedReader.readLine()) != null) {
                    if (line.contains(processName)) {
                        return true;
                    }
                }
                return false;
            } catch (Exception ex) {
                ex.printStackTrace();
                return false;
            } finally {
                if (bufferedReader != null) {
                    try {
                        bufferedReader.close();
                    } catch (Exception ex) {
                    }
                }
            }
        }
    
        private static void killLinuxProc() {
            System.out.println("关闭进程:" + "nginx.exe");
            String[] command = new String[1];
            command[0] = "pkill -9 nginx";
            System.err.println("nginx.exe" + "线程关闭失败");
    
        }
    
        /**
         * 打印进程的状态
         * 
         * @param programName1
         */
        public static void logStatus() {
            boolean flag = findProcess("nginx.exe");
            if (flag) {
                System.out.println();
                System.err.println("nginx.exe" + "进程状态:开启");
                System.out.println();
            } else {
                System.out.println();
                System.err.println("nginx.exe" + "进程状态:关闭");
                System.out.println();
            }
        }
    
        public static void testWindows() {
    
            logStatus();
    
            // 关闭进程
            killProc();
    
            logStatus();
    
            // 开启进程
            startProc();
    
            logStatus();
        }
    
        public static void testLinux() {
    
            // logStatus();
    
            // 关闭进程
            // killLinuxProc();
    
            // logStatus();
    
            // 开启进程
            startLinuxProc();
    
            // logStatus();
        }
    
        private static void startLinuxProc() {
            System.out.println("开启进程:" + "nginx");
            String command1 ="/usr/local/nginx/sbin/nginx";
            
            try {
                String pro = executeCmd2(command1);
                System.out.println(pro);
            } catch (IOException e) {
                e.printStackTrace();
                System.err.println("nginx开启失败");
            }
        }
        
        public static void main(String[] args) throws IOException {
            // testWindows();
            testLinux();
    
        }
    
    }
  • 相关阅读:
    分布式的并行计算技术
    深入学习 jQuery 选择器系列第三篇——过滤选择器之索引选择器
    JavaScript--我所理解的原型链
    JavaScript--我所理解的闭包
    JavaScript--函数的形参和函数内部声明的变量或函数重名时的问题
    JavaScript--substring 和 substr 方法手记
    CSS 和 CSS3 中的 background
    CSS3的 border-radius 和 box-shadow 及 text-shadow
    CSS3-渐变这个属性
    CSS中的伪类和伪元素回顾
  • 原文地址:https://www.cnblogs.com/excellencesy/p/11261000.html
Copyright © 2011-2022 走看看