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