我已經編寫了一個代碼來檢查HSQL,JBoss,Radius(AAA服務器)和MySQL是否在運行。代碼被寫入inifinte while循環現在我測試瞭如果這個服務(假設hsql)正在運行,那麼/bin/bash -c ps -ef | grep 'hsql' | wc -l
將返回3,當它作爲Runtime.exec()
方法中的參數傳遞時。現在我已經知道分叉進程的輸入流或errstream不能溢出。否則會導致死鎖。Runtime.exec問題Process.getInputStream和Process.waitFor()
牢記這一點我寫了這個代碼:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
public class MonitorHsqlJBossRadius {
public static void main(String[] args) {
String hsqlCmd[] = {"/bin/bash","-c","ps -ef | grep 'hsql' | wc -l "};
String jbossCommand[] = {"/bin/sh","-c","ps -ef | grep 'jboss' | wc -l"};
String radiusCommand[] = {"/bin/sh","-c","ps -ef | grep 'radius' | wc -l"};
String mySqlCommand[] = {"/bin/sh","-c","/etc/init.d/mysqld status"};
String line = null;
String mobNo = "(obscured)";
Process process = null;
Runtime runtime = Runtime.getRuntime();
BufferedReader reader = null;
SendSMS sender = new SendSMS();
boolean sendMsgHsql = false;
boolean sendMsgJBoss = false;
boolean sendMsgRadius = false;
int itr = 1;
while(true)
{
try
{
process = runtime.exec(hsqlCmd);
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while((line=reader.readLine())!=null)
{
if (!line.equals("3"))
sendMsgHsql = true;
}
process.waitFor();
if (sendMsgHsql)
{
//sender.sendSMS("HSQL is not running "+(new Date().toString()), mobNo,1,itr);
System.out.println("HSQL is not running "+itr);
}
else
System.out.println("HSQL is running "+itr);
sendMsgHsql = false;
process = runtime.exec(jbossCommand);
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while((line=reader.readLine())!=null)
{
if (!line.equals("3"))
sendMsgJBoss = true;
}
process.waitFor();
if (sendMsgJBoss)
{
//sender.sendSMS("JBoss is not running "+(new Date().toString()), mobNo,2,itr);
System.out.println("JBoss is not running "+itr);
}
else
System.out.println("JBoss is running "+itr);
sendMsgJBoss = false;
process = runtime.exec(radiusCommand);
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while((line=reader.readLine())!=null)
{
if(!line.equals("3"))
sendMsgRadius = true;
}
process.waitFor();
if (sendMsgRadius)
{
//sender.sendSMS("Radius is not running "+(new Date().toString()), mobNo,3,itr);
System.out.println("Radius is not running "+itr);
}
else
System.out.println("Radius is running "+itr);
sendMsgRadius = false;
}
catch (IOException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
try
{
System.out.println("\n--------- "+itr+" ------------\n");
itr++;
Thread.sleep(1000*5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
我期待的輸出如下
HSQL is running/not running 1 JBoss is running/not running 1 MySQL is running/not running 1 -------------------1---------------- HSQL is running/not running 2 JBoss is running/not running 2 MySQL is running/not running 2 -------------------2----------------
等等...
Howevere我」沒有像這樣得到輸出。有時HSQL的狀態消息不會被打印,或者有時JBoss狀態消息不會被打印。即使有時我得到的輸出像
------------------1----------------------
打印此狀態消息後。我想說的是,這似乎是一些競爭條件或一些同步問題。
對不起,未格式化的代碼發佈.... –
請以可讀的方式格式化您的代碼。你可以在這裏看到如何做到這一點:http://stackoverflow.com/editing-help#code –