java控制tomcat的启动和停止(包含linux模式)
2014-06-09 17:26
399 查看
tomcat目录结构:
tomcat
--bin
--EPG
--EPG-201403120000.jar
--startup.sh
在windows下将tomcat注册为服务(注册为服务的方式可以百度下),启动和停止采用"net start/stop 服务名"的方式。
对于linux或者aix,当前类的路径在上图中的jar包中,可以通过相对路径找到startup.sh进行启动;停止时,用shutdown.sh有时候不能真正的停止进程,需要找到对应的进程号,然后用程序杀掉。代码如下:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class TomcatController {
public static void startTomcatService() {
if (isOsWindows()) {
excuteRutimeCommand("net start " + Configure.tomcatServiceName);
} else {
startEPSInLinux();
}
}
public static void stopTomcatService() {
if (isOsWindows()) {
excuteRutimeCommand("net stop " + Configure.tomcatServiceName);
} else {
stopEPSInLinux();
}
}
private static List excuteRutimeCommand(String command) {
System.out.println(command);
List exeResultList = new ArrayList();
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
exeResultList.add(line);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return exeResultList;
}
private static boolean isOsWindows() {
Properties prop = System.getProperties();
String os = prop.getProperty("os.name");
System.out.println(os);
if (os.startsWith("win") || os.startsWith("Win")) {
return true;
}
return false;
}
private static void startEPSInLinux() {
List exeResultList = excuteRutimeCommand(new String[] { "/bin/sh",
"-c", "ps -ef|grep EPS/bin/bootstrap.jar|awk '{print $2}'" });
int epsProcessNum = 0;
for (int i = 0; i < exeResultList.size(); i++) {
String line = (String) exeResultList.get(i);
if (null == line || line.trim().equalsIgnoreCase("")) {
continue;
}
try {
Long.parseLong(line.trim());
epsProcessNum++;
} catch (Exception e) {
}
}
if (epsProcessNum >= 2) {
return;
}
excuteRutimeCommand("../startup.sh");
}
private static void stopEPSInLinux() {
List exeResultList = excuteRutimeCommand(new String[] { "/bin/sh",
"-c", "ps -ef|grep EPS/bin/bootstrap.jar|awk '{print $2}'" });
for (int i = 0; i < exeResultList.size(); i++) {
String line = (String) exeResultList.get(i);
if (null == line || line.trim().equalsIgnoreCase("")) {
continue;
}
try {
Long.parseLong(line);
excuteRutimeCommand("kill -9 " + line);
} catch (Exception e) {
}
}
}
private static List excuteRutimeCommand(String[] command) {
System.out.println(command[2]);
List exeResultList = new ArrayList();
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
exeResultList.add(line);
}
br.close();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return exeResultList;
}
public static void main(String[] args) throws Exception {
Configure.load();
startTomcatService();
stopTomcatService();
}
}
tomcat
--bin
--EPG
--EPG-201403120000.jar
--startup.sh
在windows下将tomcat注册为服务(注册为服务的方式可以百度下),启动和停止采用"net start/stop 服务名"的方式。
对于linux或者aix,当前类的路径在上图中的jar包中,可以通过相对路径找到startup.sh进行启动;停止时,用shutdown.sh有时候不能真正的停止进程,需要找到对应的进程号,然后用程序杀掉。代码如下:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class TomcatController {
public static void startTomcatService() {
if (isOsWindows()) {
excuteRutimeCommand("net start " + Configure.tomcatServiceName);
} else {
startEPSInLinux();
}
}
public static void stopTomcatService() {
if (isOsWindows()) {
excuteRutimeCommand("net stop " + Configure.tomcatServiceName);
} else {
stopEPSInLinux();
}
}
private static List excuteRutimeCommand(String command) {
System.out.println(command);
List exeResultList = new ArrayList();
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
exeResultList.add(line);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return exeResultList;
}
private static boolean isOsWindows() {
Properties prop = System.getProperties();
String os = prop.getProperty("os.name");
System.out.println(os);
if (os.startsWith("win") || os.startsWith("Win")) {
return true;
}
return false;
}
private static void startEPSInLinux() {
List exeResultList = excuteRutimeCommand(new String[] { "/bin/sh",
"-c", "ps -ef|grep EPS/bin/bootstrap.jar|awk '{print $2}'" });
int epsProcessNum = 0;
for (int i = 0; i < exeResultList.size(); i++) {
String line = (String) exeResultList.get(i);
if (null == line || line.trim().equalsIgnoreCase("")) {
continue;
}
try {
Long.parseLong(line.trim());
epsProcessNum++;
} catch (Exception e) {
}
}
if (epsProcessNum >= 2) {
return;
}
excuteRutimeCommand("../startup.sh");
}
private static void stopEPSInLinux() {
List exeResultList = excuteRutimeCommand(new String[] { "/bin/sh",
"-c", "ps -ef|grep EPS/bin/bootstrap.jar|awk '{print $2}'" });
for (int i = 0; i < exeResultList.size(); i++) {
String line = (String) exeResultList.get(i);
if (null == line || line.trim().equalsIgnoreCase("")) {
continue;
}
try {
Long.parseLong(line);
excuteRutimeCommand("kill -9 " + line);
} catch (Exception e) {
}
}
}
private static List excuteRutimeCommand(String[] command) {
System.out.println(command[2]);
List exeResultList = new ArrayList();
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
exeResultList.add(line);
}
br.close();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return exeResultList;
}
public static void main(String[] args) throws Exception {
Configure.load();
startTomcatService();
stopTomcatService();
}
}
相关文章推荐
- java程序监控tomcat中部署的项目的状态以及控制某些项目的启动停止
- java程序监控tomcat中部署的项目的状态以及控制某些项目的启动停止
- Linux下启动Tomcat出现多个java进程
- shell脚本控制jar的启动和停止 - Linux,shell,脚本控制jar包启动停止 - language - ITeye论坛
- linux下启动tomcat的时候提示java.net.UnknownHostException的解决办法
- Linux下启动停止查看杀死Tomcat进程
- linux中tomcat启动错误:java.net.BindException: address already in use <null>:8443
- 使用Java Service Wrapper设置tomcat作为linux服务并且开机自动启动
- linux中tomcat启动错误:java.net.BindException: Permission denied <null>:80
- linux下用脚本监测tomcat停止并且自动启动
- Linux下tomcat的快速启动,停止,重启脚本
- 14-linux中mysql和tomcat的启动和停止命令
- java代码实现tomcat的启动和停止
- JAVA类项目如何开启远程DEBUG模式?TOMCAT,JETTY等容器启动也可以DEBUG,以及JAVA远程连接JMXREMOTE
- 使用Java Service Wrapper设置tomcat作为linux服务并且开机自动启动
- Linux 下Tomcat集群 及服务模式启动Tomcat
- java控制tomcat6.0启动关闭
- linux 启动和停止tomcat(解决服务已关闭,进程未停止的问题)
- linux下启动tomcat的时候提示java.net.UnknownHostException的解决办法
- Linux下Debug模式启动Tomcat远程调试