在阿里云上弄了个最小的云服务器,512m内存,建了个百度网盘资源站省淘网,tomcat有时会因内存占用过多,被系统回收,暂时没有太好的解决方法,就写了个小程序,监控tomcat线程,如果线程没了,就自动启动,代码如下:
package monitor;
import java.io.ByteArrayOutputStream;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.PumpStreamHandler;
public class Monitor {
public static void main(String[] args) {
try {
while (true) {
Thread.sleep(10000);
String command = "ps -ef";
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
CommandLine commandline = CommandLine.parse(command);
DefaultExecutor exec = new DefaultExecutor();
exec.setExitValue(0);
PumpStreamHandler streamHandler = new PumpStreamHandler(
errorStream);
exec.setStreamHandler(streamHandler);
exec.execute(commandline);
String result = errorStream.toString("UTF-8");
if (result.indexOf("tomcat") < 0) {
System.out.println("未找到进程,启动tomcat");
command = "./startup.sh";
commandline = CommandLine.parse(command);
exec.execute(commandline);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
需要依赖apache的commons-exec-1.2.jar包
当时对linux系统不熟,才写的这个,现在已经改为用linux crontab的方式了