java 线程开启关闭吗_java代码开启关闭线程(nginx)

importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;public classdemo3 {/*** @desc 启动进程 10.158 root root*/

public static voidstartProc() {

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 voidkillProc() {

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) throwsIOException {

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= newStringBuilder();while ((line = br.readLine()) != null) {

System.out.println(line);

build.append(line);

}returnbuild.toString();

}/*** @desc 执行cmd命令*/

public static String executeCmd2(String command) throwsIOException {

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= newStringBuilder();while ((line = br.readLine()) != null) {

System.out.println(line);

build.append(line);

}returnbuild.toString();

}/*** @desc 判断进程是否开启*/

public static booleanfindProcess(String processName) {

BufferedReader bufferedReader= null;try{

Process proc= Runtime.getRuntime().exec("tasklist -fi " + '"' + "imagename eq " + processName + '"');

bufferedReader= new BufferedReader(newInputStreamReader(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 voidkillLinuxProc() {

System.out.println("关闭进程:" + "nginx.exe");

String[] command= new String[1];

command[0] = "pkill -9 nginx";

System.err.println("nginx.exe" + "线程关闭失败");

}/*** 打印进程的状态

*

*@paramprogramName1*/

public static voidlogStatus() {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 voidtestWindows() {

logStatus();//关闭进程

killProc();

logStatus();//开启进程

startProc();

logStatus();

}public static voidtestLinux() {//logStatus();//关闭进程//killLinuxProc();//logStatus();//开启进程

startLinuxProc();//logStatus();

}private static voidstartLinuxProc() {

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) throwsIOException {//testWindows();

testLinux();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值