免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3934 | 回复: 5
打印 上一主题 下一主题

使用Java查询shell下进程数的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-09-05 17:15 |只看该作者 |倒序浏览
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class test
{

        public static void main(String[] args)
        {

                try
                {
                        String[] cmd =
                        { "bash", "-c", "ps", "aux|grep", "-ci", "procName" };
                        Process proc = Runtime.getRuntime().exec(cmd);
                        BufferedReader in = new BufferedReader(new InputStreamReader(proc
                                .getInputStream()));

                        String string_Temp = in.readLine();
                        while (string_Temp != null)
                        {
                                System.out.println(string_Temp);
                                string_Temp = in.readLine();
                        }
                }
                catch (Exception e)
                {
                }
        }

}

代码如上,本意是查询进程名称为procName的进程数量,在shell中直接执行会输出数字,但是我在java中执行打印出的却是进程列表,似乎grep没有执行到。

论坛徽章:
0
2 [报告]
发表于 2007-09-06 10:39 |只看该作者
java程序以什么用户身份执行?

完整命令   bash -c 'ps -aux|grep -ci $procName'

你这个数组写得不对

论坛徽章:
0
3 [报告]
发表于 2007-09-06 11:05 |只看该作者
root用户执行

我也试过这样
Runtime.getRuntime.exec("bash -c  \'ps -aux|grep -ci $procName\' ");
但是执行的结果是没有打印出任何内容到console

Runtime.getRuntime.exec("bash -c  \"ps -aux|grep -ci $procName\" ");也是同样的

论坛徽章:
0
4 [报告]
发表于 2007-09-06 11:47 |只看该作者
String[] cmd =  { "bash", "-c", "ps -aux|grep -ci $procName" };

论坛徽章:
0
5 [报告]
发表于 2007-09-06 13:50 |只看该作者
o.....谢谢!

论坛徽章:
0
6 [报告]
发表于 2007-09-06 13:51 |只看该作者
不过还有个奇怪的现象,我在eclipse中运行得到 2,在shell中直接执行ps -aux|grep -ci $procName 得到 1
。。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP