博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java代码开启关闭线程(nginx)
阅读量:4363 次
发布时间:2019-06-07

本文共 4460 字,大约阅读时间需要 14 分钟。

源码:

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();    }}

 

转载于:https://www.cnblogs.com/excellencesy/p/11261000.html

你可能感兴趣的文章
session和cookie的区别
查看>>
alert弹出窗口,点击确认后关闭页面
查看>>
oracle问题之数据库恢复(三)
查看>>
单点登陆(SSO)
查看>>
HR,也确实“尽职尽责”
查看>>
MaxComputer 使用客户端配置
查看>>
20190823 顺其自然
查看>>
阅读《余生有你,人间值得》有感
查看>>
每日英语
查看>>
SpringCloud+feign 基于Springboot2.0 负载均衡
查看>>
【BZOJ5094】硬盘检测 概率
查看>>
大庆金桥帆软报表案例
查看>>
Proxy模式
查看>>
读书多些会怎样
查看>>
浏览器好用的技术
查看>>
HDU 2188------巴什博弈
查看>>
tp5任务队列使用supervisor常驻进程
查看>>
Xmind?
查看>>
spring+quartz 实现定时任务三
查看>>
day2-三级菜单
查看>>