怎样采用java实现对系统进程的监控

tanlcn 2010-11-21 10:45:11
怎样采用java实现对系统进程的监控
比如,我指定一个进程名,与进程路径,
如果系统中该进程在运行,而且进程路径=指定的进程路径
就返回yes,否则就是no

系统:windows2k, windows 2003,windows xp, suse linux 9+
...全文
1120 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
chyx413332087 2011-06-26
  • 打赏
  • 举报
回复
mark
fly的狐狸 2011-06-25
  • 打赏
  • 举报
回复
可以写shell实现
--------------------
#!/bin/bash
kill -9 `ps -ef|grep -v grep|grep 进程名字|awk '{print $2}'`
这个可以杀掉你想杀的进程
---------------------
#!/bin/bash
sh 启动进程绝对路径 &
这个可以启动你要的进程
---------------------
ps -ef | grep 进程名
这个就可以查看你说要的进程所有信息

同一目录运行多个tomcat就用进程名区分啊
chenchenyangll 2011-06-18
  • 打赏
  • 举报
回复
Mark一下,学习
kanglishan 2011-06-18
  • 打赏
  • 举报
回复
我也正在写这个东西
  • 打赏
  • 举报
回复
java 不适合做这些开发,如果你一定要这样用的话,哎!
tanlcn 2011-02-10
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 gaoyuan21810 的回复:]
wmic process where name="java.exe" get /format:value
这个可以显示进程所有信息
你可以看看里面的commandline那个属性,里面记录了启动这个java的原始路径
也可以用命令
wmic process where name="java.exe" get commandline
来直接获得commandline

不过感觉还得考虑……
[/Quote]
恩。
wmic在windows 2003 ,xp上都是可以的,win2000不支持wmic
ilrxx 2011-02-10
  • 打赏
  • 举报
回复
Runtime windows下执行cmd,linux下执行的是sh,就是shell脚本,写个shell脚本,用java去调用。。我感觉java太万能了。。
gaoyuan21810 2010-12-23
  • 打赏
  • 举报
回复
wmic process where name="java.exe" get /format:value
这个可以显示进程所有信息
你可以看看里面的commandline那个属性,里面记录了启动这个java的原始路径
也可以用命令
wmic process where name="java.exe" get commandline
来直接获得commandline

不过感觉还得考虑系统有没有启用wmic命令把?
gaoyuan21810 2010-12-23
  • 打赏
  • 举报
回复
请问楼上会不会遇到wmic命令不能用的情况啊?
tanlcn 2010-12-08
  • 打赏
  • 举报
回复
public boolean isWindowsProcessExist(String processName, String processInfo) {
// TODO Auto-generated method stub
String[] cmd = new String[] { "cmd.exe", "/C", "wmic process where name='" + processName.trim() + "' get executablepath" };
BufferedReader bReader = null;

Process process;
try {
process = Runtime.getRuntime().exec(cmd);
process.getOutputStream().close();
bReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while((line = bReader.readLine())!=null)
{
if(line.trim().equals(processInfo.trim()))
return true;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}

public boolean isLinuxProcessExist(String procName, String procPath) {
// TODO Auto-generated method stub
String cmd = null ;
cmd = "ps -ef | grep '" + procName.trim() + "' | grep -v grep | awk '{print $2}'";
String[] shellCmd1 = new String[] { "/bin/bash", "-c", cmd };
String[] shellCmd2 = null;
shellCmd2 = new String[] { "/bin/bash", "-c", cmd };
BufferedReader bReader = null;
List<String> processIDList = null;
Process process;
try {
//找到根据进程名获取到的进程号列表
process = Runtime.getRuntime().exec(shellCmd1);
process.getOutputStream().close();
bReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
processIDList = new ArrayList<String>();
while((line = bReader.readLine())!=null)
{ processIDList.add(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(bReader != null)
bReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

for(int i = 0; i< processIDList.size(); i++)
{
shellCmd2 = new String[] { "/bin/bash", "-c", "lsof -p " + processIDList.get(i) + " | grep 'txt' |grep -v grep | awk '{print $NF}'" };
try {
process = Runtime.getRuntime().exec(shellCmd2);
process.getOutputStream().close();
bReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while((line = bReader.readLine())!=null)
{
if(line.trim().equals(procPath.trim()))
return true;
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if(bReader != null)
bReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return false;
}


但是,又遇到了新问题:

4. 在判断进程时,要考虑到不同进程的执行路径是一样的情况,如:
运行多个tomcat,他们启动的java路径是一样的,杀死一个会影响到其他的tomcat。
//这个是一个异常情况.现在如果是在同一个机器上启动了多个tomcat按照现有的实现方式,是不能准确的区分出对应的监控进程
//例如:<processInfo name="javaw">/usr/java/java</processInfo > 这个是不能检测出我的那个tomcat挂掉了,所以在<processInfo>对应的值里面需要填写tomcat bootstrap类启动的路径来检测
谁能解释下,该如何处理?
程序新视界 2010-11-26
  • 打赏
  • 举报
回复
java擅长这方面吗?
tanlcn 2010-11-26
  • 打赏
  • 举报
回复
恩。。产品经理是要求可以用shell脚本,然后Java调用脚本执行的结果,
但是之前没有接触过shell脚本,以及java执行脚本
所以哪位大侠可以给出个demo啊?
evangelionxb 2010-11-26
  • 打赏
  • 举报
回复
分我收下了。
楼主告诉你个关键字:sigar
开源。
qingyuan18 2010-11-24
  • 打赏
  • 举报
回复
这种一般是跟操作系统绑定得很紧的,用java的API没什么很大的用处,最好调用系统命令,比如Unix下的ps -ef|grep "你的进程名",Windows下的tasklist
sunyiz 2010-11-24
  • 打赏
  • 举报
回复
Linux..
这个还真不熟
貌似只记得是什么
ps -ef
kill
一类的命令吧
tanlcn 2010-11-24
  • 打赏
  • 举报
回复
根据sunyiz的回复,初步搞出了windows下监控的功能,代码如下:

String[] cmd = new String[] { "cmd.exe", "/C", "wmic process " };
BufferedReader bReader = null;
try {
process = Runtime.getRuntime().exec(cmd);
process.getOutputStream().close();
bReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while((line = bReader.readLine())!=null)
{
//如果该进程存在于进程列表中
String [] words = line.split("\\s+");
if((words[0].indexOf(processName))!=-1)
{
//如果该进程与配置的进程路径相符
if((words[1].indexOf(processInfo))!=1)
{ return true;}
}}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但这只是windows下的方案,但是Linux下呢?所以等结贴时我会给你一半的分数哦
diggywang 2010-11-23
  • 打赏
  • 举报
回复
用java搞监控系统进程,疯了?
tonytone2008 2010-11-22
  • 打赏
  • 举报
回复
高级货,搞不懂……
sunyiz 2010-11-22
  • 打赏
  • 举报
回复
Runtime runtime = Runtime.getRuntime();
runtime.exec("cmd /c wmic process");

试试这个呢

tanlcn 2010-11-22
  • 打赏
  • 举报
回复
楼上,你只实现了查看该进程是否存在的功能
而我所提的需求,还要检测该进程的路径是否=指定的进程路径
因为有可能别的进程所起的名字=所配置的进程名称
加载更多回复(1)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧