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(); } }
java代码开启关闭线程(nginx)的更多相关文章
- [改善Java代码]优先选择线程池
在Java1.5之前,实现多线程编程比较麻烦,需要自己启动线程,并关注同步资源,防止线程死锁等问题,在1.5版本之后引入了并行计算框架,大大简化了多线程开发. 我们知道线程有5个状态:新建状态(New ...
- Windows和Linux如何使用Java代码实现关闭进程
在用selenium做自动化测试时,由于各种不明原因,有时Chrome浏览器会出现假死的情况,也就是整个浏览器响应超时,本人脚本主要部署在Windows机器上,所以主要以Windows为主,浏览器为C ...
- JDK中ThreadDump诊断Java代码中的线程死锁问题
多线程的死锁..死锁不是死了而是线程互相等待... 在项目中可能就是在几十万行的代码中存在一个死锁的问题,如何发现这个问题并且解决这个问题. JavaJDK为我们提供了一个诊断工具叫做ThreadDu ...
- Java代码启动/关闭进程
ProcessBuilder builder = new ProcessBuilder(命令,参数,参数...); Process process = builder.start(); br = ne ...
- Jni层回调java代码【转】
本文转载自:http://www.linuxidc.com/Linux/2014-03/97562.htm JNI是Java Native Interface的缩写,是Java平台的重要特性,使得Ja ...
- Java语言定义的线程状态分析
说到线程,一定要谈到线程状态,不同的状态说明线程正处于不同的工作机制下,不同的工作机制下某些动作可能对线程产生不同的影响. Java语言定义了6中状态,而同一时刻,线程有且仅有其中的一种状态.要获取J ...
- Java高并发之线程基本操作
结合上一篇同步异步,这篇理解线程操作. 1.新建线程.不止thread和runnable,Callable和Future了解一下 package com.thread; import java.tex ...
- Java多线程学习篇——线程的开启
随着开发项目中业务功能的增加,必然某些功能会涉及到线程以及并发编程的知识点.笔者就在现在的公司接触到了很多软硬件结合和socket通讯的项目了,很多的功能运用到了串口通讯编程,串口通讯编程的安卓端就是 ...
- [改善Java代码]不使用stop方法停止线程
线程启动完毕后,在运行可能需要终止,Java提供的终止方法只有一个stop,但是不建议使用此方法,因为它有以下三个问题: (1)stop方法是过时的 从Java编码规则来说,已经过时的方式不建议采用. ...
随机推荐
- 【C语言】输入10个人的成绩,求平均值
#include<stdio.h> int main() { int i; ,score[]; printf("请输入10个数字:\n"); ; i < ; i+ ...
- docker 初识1
学习网址 https://git.oschina.net/yangllsdev/docker-training https://docs.docker.com/engine/installation/ ...
- 出现xxxtbox问题又有集群网络等问题时的解决
出现xxxtbox问题又有集群网络等问题时的解决 集群环境本身问题引发 重新rancher上删除集群,正常构建集群成功,环境就没问题,否则由于环境问题怎么找解决方案都有问题
- 一个超几何函数$_3F_2$的积分
\[\Large\displaystyle \int_0^\infty{_3F_2}\left(\begin{array}c\dfrac58,\dfrac58,\dfrac98\\\dfrac12,\ ...
- Linux下调试caffe
参考博客:https://blog.csdn.net/xiaoyezi_1834/article/details/50724875 使用Anjuta 我使用的是ubuntu18.04,安装命令: su ...
- expect 脚本
实现远程执行 /home/dataexa/test/proxy.expect touch proxy.expect #!/usr/bin/expect set timeout 30 spawn ssh ...
- C语言当中int,float,double,char这四个有什么区别?
区别在以下方面: 一.定义方面: 1.int为整数型,用于定义整数类型的数据 . 2.float为单精度浮点型,能准确到小数点后六位 . 3.double为双精度浮点型,能准确到小数点都十二位 . 4 ...
- 每天进步一点点------SOPC TIMER (一)
寄存器图 可以通过操作以下的寄存器来实现对timer(定时器)内核的操作(仅描述32位计数器) 状态寄存器: TO(timeout) :计数器计数到0时,该位置1,之后TO位的值会保持,直到手动清零, ...
- yum grouplist 安装gnome桌面环境
经常,我们如果需要安装一些比较复杂的软件时,都会在安装操作系统的时候直接勾选,然后进行安装.但是,有的时候,等操作系统安装完了才发现有遗漏的软件没有安装. 这个时候,yum就要出来救场了.使用yu ...
- PAT 1010 Radix (二分)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...