源码:

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

java代码开启关闭线程(nginx)的更多相关文章

  1. [改善Java代码]优先选择线程池

    在Java1.5之前,实现多线程编程比较麻烦,需要自己启动线程,并关注同步资源,防止线程死锁等问题,在1.5版本之后引入了并行计算框架,大大简化了多线程开发. 我们知道线程有5个状态:新建状态(New ...

  2. Windows和Linux如何使用Java代码实现关闭进程

    在用selenium做自动化测试时,由于各种不明原因,有时Chrome浏览器会出现假死的情况,也就是整个浏览器响应超时,本人脚本主要部署在Windows机器上,所以主要以Windows为主,浏览器为C ...

  3. JDK中ThreadDump诊断Java代码中的线程死锁问题

    多线程的死锁..死锁不是死了而是线程互相等待... 在项目中可能就是在几十万行的代码中存在一个死锁的问题,如何发现这个问题并且解决这个问题. JavaJDK为我们提供了一个诊断工具叫做ThreadDu ...

  4. Java代码启动/关闭进程

    ProcessBuilder builder = new ProcessBuilder(命令,参数,参数...); Process process = builder.start(); br = ne ...

  5. Jni层回调java代码【转】

    本文转载自:http://www.linuxidc.com/Linux/2014-03/97562.htm JNI是Java Native Interface的缩写,是Java平台的重要特性,使得Ja ...

  6. Java语言定义的线程状态分析

    说到线程,一定要谈到线程状态,不同的状态说明线程正处于不同的工作机制下,不同的工作机制下某些动作可能对线程产生不同的影响. Java语言定义了6中状态,而同一时刻,线程有且仅有其中的一种状态.要获取J ...

  7. Java高并发之线程基本操作

    结合上一篇同步异步,这篇理解线程操作. 1.新建线程.不止thread和runnable,Callable和Future了解一下 package com.thread; import java.tex ...

  8. Java多线程学习篇——线程的开启

    随着开发项目中业务功能的增加,必然某些功能会涉及到线程以及并发编程的知识点.笔者就在现在的公司接触到了很多软硬件结合和socket通讯的项目了,很多的功能运用到了串口通讯编程,串口通讯编程的安卓端就是 ...

  9. [改善Java代码]不使用stop方法停止线程

    线程启动完毕后,在运行可能需要终止,Java提供的终止方法只有一个stop,但是不建议使用此方法,因为它有以下三个问题: (1)stop方法是过时的 从Java编码规则来说,已经过时的方式不建议采用. ...

随机推荐

  1. Verilog 编写规范

    在学习Python时,作者有一句话对我影响很大.作者希望我们在学习编写程序的时候注意一些业内约定的规范.在内行人眼中,你的编写格式,就已经暴露了你的程度.学习verilog也是一样的道理,一段好的ve ...

  2. kali 安装google输入法

    脑子一热装了一礼拜的kali,在20多遍的重装后终于成功了 先码一篇如何安装google输入法 首先得更新源,用leafpad /etc/apt/sources.list打开,或vi也可以,更新源百度 ...

  3. spring cloud config 连接GitHub访问 报错 Cannot clone or checkout repository

    原因是建立仓库的时候将仓库私有化了,将仓库公有 或者 设置账号密码即可!

  4. idea中使用Data Source and Drivers时,如果使用自己自定义的jar包

  5. 计算几何-点与多边形的位置判断-zoj1081Points Within

    This article is made by Jason-Cow.Welcome to reprint.But please post the writer's address. http://ww ...

  6. TCP和UDP的一些注意事项

    TCP的一些注意事项 1. tcp服务器一般情况下都需要绑定,否则客户端找不到这个服务器,更无法链接到服务器 2. tcp客户端一般不绑定,因为是主动链接服务器,所以只要确定好服务器的ip.port等 ...

  7. Linux - curl 基本使用

    1. 概述 我接触过的很多服务端调试, 接口测试, 最终都落到了这个地方 简答介绍 curl 的使用 尽量循序渐进, 因为我也不太熟悉 大概会提到的命令 curl curl -v curl -s cu ...

  8. Mac电脑安装openresty

    安装brew 软件仓库, /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/ ...

  9. 陆金所退出市场,我说:趁现在,抓紧离开P2P市场,你赞同吗?

    编辑 | 于斌 出品 | 于见(mpyujian) 18日,也就是前天,陆金所退出P2P市场的消息就像颗"重磅炸弹"一样,一波激起千层浪,陆金所作为全国最大财富平台之一,这次退出, ...

  10. etc/hosts文件详解

    Linux 修改 etc/hosts文件 hosts文件 hosts —— the static table lookup for host name(主机名查询静态表). hosts文件是Linux ...