一,判断程序的部署环境是nginx还是windows

    /**
* 判断操作系统是不是windows
*
* @return true:是win false:是Linux
*/
public static boolean judgeOs() {
String os = System.getProperty("os.name").toLowerCase();
if (os != null && os.startsWith("windows")) {
return true;
} else {
return false;
}
}

二,开启nginx

(1)开启windows系统的ngin

    /**
* 开启windows系统的nginx
* @throws IOException
*/
private static void startWinProc() throws IOException{
String myExe = "cmd /c start nginx";
String CONFPREFIXURL = System.getProperty("user.dir") + File.separator + "nginx"
+ File.separator + "windows"; File dir = new File(CONFPREFIXURL);
String[] str = new String[] {};
// 执行命令
Runtime.getRuntime().exec(myExe, str, dir);
}

(2)开启linux系统的nginx

    /**
* 开启linux的nginx
* @throws IOException
*/
private static void startLinuxProc() throws IOException {
System.out.println("开启进程:" + "nginx");
String command1 = "/usr/local/nginx/sbin/nginx"; String pro = executeCmd2(command1);
System.out.println(pro);
}

(3)跨平台开启Nginx

    /**
* 跨平台启动nginx
*
* @throws IOException
*/
private static void startProc() throws IOException {
if (judgeOs()) {
startWinProc();
} else {
startLinuxProc();
}
}

三,关闭nginx

(1)关闭windows系统的nginx

    /**
*关闭windows系统的nginx
* @throws IOException
*/
public static void KillWin() throws IOException{
executeCmd("taskkill /F /IM " + "nginx.exe");
}

(2)关闭linux系统的nginx

    /**
* 关闭linux系统的nginx
* @throws IOException
*/
private static void killLinuxProc() throws IOException {
String command = "pkill -9 nginx";
executeCmd(command);
}

(3)nginx的跨平台关闭

    /**
* 跨平台关闭nginx
* @throws IOException
*/
public static void killProc() throws IOException {
if(judgeOs()){
KillWin();
}else{
killLinuxProc();
}
}

四,获取项目所在的磁盘位置

        String CONFPREFIXURL = System.getProperty("user.dir");
System.out.println(CONFPREFIXURL);

五,获取项目所在主机ip

    /**
* 获取项目部署环境的ip
* @return
*/
public static String getIP() {
String url = "";;
try {
for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements(); ) {
NetworkInterface item = e.nextElement();
for (InterfaceAddress address : item.getInterfaceAddresses()) {
if (item.isLoopback() || !item.isUp()) {
continue;
}
if (address.getAddress() instanceof Inet4Address) {
Inet4Address inet4Address = (Inet4Address) address.getAddress();
url =inet4Address.getHostAddress();
}
}
}
//url = InetAddress.getLocalHost().getHostAddress();
} catch (SocketException e) {
throw new RuntimeException(e);
}
return url;
}

更多获取系统变量的方法:https://www.cnblogs.com/excellencesy/p/11278889.html

六,完整工具类

package com.googosoft.until;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List; public class NginxUtil { /**
*
* @return 1:代理重启成功 2:进程未开启 3:进程关闭时出错 4:进程启动时出错
*/
public static int reStartProc() { int flag = 0;
if (findProcess()) {
// 进程没开启
try {
killProc();
} catch (IOException e) {
e.printStackTrace();
System.err.println("nginx.exe" + "线程开启失败");
} finally {
if (findProcess()) {
flag = 3;// 关闭失败
}
} try {
startProc();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (findProcess()) {
flag = 1;// 开启成功
} else {
flag = 4;// 启动失败
}
} } else {
// 进程未开启
flag = 2;
}
return flag;
} /**
* 关闭nginx进程
*
* @return 1:进程关闭成功 2:进程没有开启 3:进程关闭失败
*/
public static int stop() {
int flag = 0; if (findProcess()) {
// 进程开启了
try {
killProc();
} catch (IOException e) {
e.printStackTrace();
System.err.println("nginx.exe" + "线程关闭失败");
} finally {
if (!findProcess()) {
flag = 1;// 关闭成功
} else {
flag = 3;// 关闭失败
}
}
} else {
// 进程没开启
flag = 2;
}
return flag; } /**
* 跨平台关闭nginx
*
* @throws IOException
*/
public static void killProc() throws IOException {
if (judgeOs()) {
KillWin();
} else {
killLinuxProc();
}
} /**
* 关闭windows系统的nginx
*
* @throws IOException
*/
public static void KillWin() throws IOException {
executeCmd("taskkill /F /IM " + "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);
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 = "nginx.exe";
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) {
}
}
}
} /**
* 关闭linux系统的nginx
*
* @throws IOException
*/
private static void killLinuxProc() throws IOException {
String command = "pkill -9 nginx";
executeCmd(command);
} /**
* 打印进程的状态
*
* @param programName1
*/
public static void logStatus() {
boolean flag = findProcess();
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();
}
} /**
* 开启linux的nginx
* @throws IOException
*/
private static void startLinuxProc() throws IOException {
System.out.println("开启进程:" + "nginx");
String command1 = "/usr/local/nginx/sbin/nginx"; String pro = executeCmd2(command1);
System.out.println(pro);
} /**
* windows平台开启
*
* @throws IOException
* 1:开启成功 2:开启失败 3:已开启
*/
public static int start() {
int flag = 0; if (!findProcess()) {
// 进程开启了
try {
startProc();
} catch (IOException e) {
e.printStackTrace();
System.err.println("nginx.exe" + "线程关闭失败");
} finally {
if (findProcess()) {
flag = 1;// 开启成功
} else {
flag = 2;// 开启失败
}
}
} else {
// 进程已经开启
flag = 3;
}
return flag;
} /**
* 跨平台启动nginx
*
* @throws IOException
*/
private static void startProc() throws IOException {
if (judgeOs()) {
startWinProc();
} else {
startLinuxProc();
}
} /**
* 开启windows系统的nginx
*
* @throws IOException
*/
private static void startWinProc() throws IOException {
String myExe = "cmd /c start nginx";
String CONFPREFIXURL = System.getProperty("user.dir") + File.separator + "nginx" + File.separator + "windows"; File dir = new File(CONFPREFIXURL);
String[] str = new String[] {};
// 执行命令
Runtime.getRuntime().exec(myExe, str, dir);
} /**
* 判断操作系统是不是windows
*
* @return true:是win false:是Linux
*/
public static boolean judgeOs() {
String os = System.getProperty("os.name").toLowerCase();
if (os != null && os.startsWith("windows")) {
return true;
} else {
return false;
}
} /**
* 获取项目部署环境的ip
*
* @return
*/
public static String getIP() {
String url = "";
;
try {
for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
NetworkInterface item = e.nextElement();
for (InterfaceAddress address : item.getInterfaceAddresses()) {
if (item.isLoopback() || !item.isUp()) {
continue;
}
if (address.getAddress() instanceof Inet4Address) {
Inet4Address inet4Address = (Inet4Address) address.getAddress();
url = inet4Address.getHostAddress();
}
}
}
// url = InetAddress.getLocalHost().getHostAddress();
} catch (SocketException e) {
throw new RuntimeException(e);
}
return url;
} /**
* 判断某个字符串是否是数字
*
* @param str
* @return
*/
public static boolean isNumer(String str) {
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
} /**
* 以行为单位读取文件,常用于读面向行的格式化文件
*
* @return
*/
public static int getPort() {
String fileName = getConfAddr();
int port = -1;
File file = new File(fileName);
BufferedReader reader = null;
String lastLine = "";
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
if (!tempString.trim().startsWith("#") && tempString.trim().length() != 0) {
// System.out.println(tempString.trim());
if ("server {".equals(lastLine.trim())) {
String content = tempString.trim();
if (content.startsWith("listen") && content.endsWith(";")) {
String number = content.replace("listen", "").replace(";", "").trim();
if (isNumer(number))
port = Integer.parseInt(number);
}
}
lastLine = tempString;
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
} return port;
} public static String getConfAddr() {
String CONFPREFIXURL = System.getProperty("user.dir") + File.separator + "nginx" + File.separator;
if (judgeOs()) {
CONFPREFIXURL = CONFPREFIXURL + "windows" + File.separator + "conf" + File.separator + "nginx.conf";
} else {
// linux的处理
}
return CONFPREFIXURL;
} public static void main(String[] args) { } }

java操作nginx的更多相关文章

  1. Java操作Sqlite数据库-jdbc连接

    Java操作Sqlite数据库步骤: 1. 导入Sqlite jdbc 本文使用sqlite-jdbc-3.7.2.jar,下载地址 http://pan.baidu.com/s/1kVHAGdD 2 ...

  2. 【MongoDB for Java】Java操作MongoDB

    上一篇文章: http://www.cnblogs.com/hoojo/archive/2011/06/01/2066426.html介绍到了在MongoDB的控制台完成MongoDB的数据操作,通过 ...

  3. Java操作Oracle

    public class DBCon { // 数据库驱动对象 public static final String DRIVER = "oracle.jdbc.driver.OracleD ...

  4. JAVA操作ORACLE数据库的存储过程

    一.任务提出 JAVA操作oracle11g存储过程实验需要完成以下几个实例: 1.调用没有返回参数的过程(插入记录.更新记录) 2.有返回参数的过程 3.返回列表的过程 4.返回带分页的列表的过程. ...

  5. JAVA操作MongoDB数据库

    1. 首先,下载MongoDB对Java支持的驱动包 驱动包下载地址:https://github.com/mongodb/mongo-java-driver/downloads 2.Java操作Mo ...

  6. Java操作Session与Cookie

    1,Java操作Session Java操作Session非常简单,步骤如下 1.1,在servlet中通过request获取session HttpSession session = request ...

  7. JAVA操作COOKIE

    JAVA操作COOKIE 1.设置Cookie Cookie cookie = new Cookie("key", "value"); cookie.setMa ...

  8. [转]MongoDB for Java】Java操作MongoDB

    原文地址: MongoDB for Java]Java操作MongoDB 开发环境: System:Windows IDE:eclipse.MyEclipse 8 Database:mongoDB 开 ...

  9. Java操作文件夹的工具类

    Java操作文件夹的工具类 import java.io.File; public class DeleteDirectory { /** * 删除单个文件 * @param fileName 要删除 ...

随机推荐

  1. 每天进步一点点------Allegro 群组布线

    执行Route->connect命令,设置好控制面板中的内容.然后设置同时走线的GROUP包含哪些网络,有两种方法.第一种方法,如果几个网络是紧邻的,可以直接框选,选中的网络就会被包含在GROU ...

  2. 每天进步一点点------Allegro 建立封装的一般步骤

    在制作封装之前,先确定你需要的焊盘,如果库中没有,那就要自己画了,(我就是自己画的) 制作二极管1N5822 SMD,实际尺寸:480milX520mil 一.添加元件焊盘 1 启动Allegro P ...

  3. Spark对接Kafka、HBase

    本项目是为网站日志流量分析做的基础:网站日志流量分析系统,Kafka.HBase集群的搭建可参考:使用Docker搭建Spark集群(用于实现网站流量实时分析模块),里面有关于该搭建过程 本次对接Ka ...

  4. MySQL-THINKPHP 商城系统二 商品模块的展示

    上回已经说到,商品被分为spu商品和sku商品  , ------------------------------------------------------------------------- ...

  5. Vue 实现todolist的添加删除功能

    直接上代码 vm.$emit( eventName, [-args] ) 触发当前实例上的事件 可选附加参数 传给监听器回调. <style> #app{ margin: 100px; } ...

  6. sublime 神一样的插件

    专属配置 // 主题 "theme": "Boxy Tomorrow.sublime-theme", "theme_grid_border_size_ ...

  7. CSP2019感想

    我觉得自己好弱啊. 想更新博客,可是又没有人看. 本来自己还不算太弱,可是自己越来越腐败. 看看自己,连更新博客的资本都没有了呢.别人写些什么都是经典干货.自己写什么自己都觉得垃圾,只好默默地删掉. ...

  8. opencv:二值图像的概念

    灰度图像与二值图像 二值分割 #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; usi ...

  9. wampserver 配置的几个坑(雾

    1. 从安装版本说起 自从我进入大学之后,便继承了学长那里的wampserver2.5版本 直到有一天自己下载wamp的时候才注意到已经有 3.0.6版本了 (现在有更高的了 但是3.0.6够用了) ...

  10. [lua]紫猫lua教程-命令宝典-L1-01-06. 循环结构

    L1[循环]01. for循环结构介绍 只是简单的说了下计数型的for循环结构 for i=1,10,1 do testlib.traceprint(i) end 注意几点: 1.上面的1和10表示循 ...