java linux 进程是否存在,Java - 如何检查另一个(非Java)进程是否在Linux上运行...

I'm having some weird problems with this.

We are using Xvfb virtual desktop manager and want to make sure it's running before I continue. Using pure shell, I could do this easily:

ps -ef | grep Xvfb | grep -v grep

And that gives me exactly what I need, a single line containing information about the Xvfb proc. Next, I want to incorporate this into my Java program and parse the results and store the PID of the running Xvfb process. So I am trying this:

String line;

try {

Process p = Runtime.getRuntime().exec("ps -ef | grep Xvfb | grep -v grep");

BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

while ((line = input.readLine()) != null)

{

System.out.println(line);

}

} catch (Exception err) {

System.out.println(err);

}

The bizarre thing is that if I use "ps -ef", I get a huge list of processes dumped to my console when I run my app. But if I use | grep to narrow the list of processes returned, I get zero results. input.readLine() gets null every time.

I have also tried:

ps -ef | grep Xvfb | grep -v grep | awk {'print $2'}

To just grab the process id. Also, no luck.

Has anyone else experienced this or know what I'm doing wrong?

解决方案

You're trying to use the "|" which is a pipe function that is particular to the shell, therefore you cannot do it in the java process. You could just try getting the process ID by using pidof Xvfb.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值