使用'command slinging'获取Groovy中另一个程序的进程ID [英] Getting the Process ID of another program in Groovy using 'command slinging'

查看:115
本文介绍了使用'command slinging'获取Groovy中另一个程序的进程ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  import java.lang.management。* 

final String name = ManagementFactory.getRuntimeMXBean()。getName();
final Integer pid = Integer.parseInt(name [0..name.indexOf(@) - 1])$ ​​b $ b

我在代码中试过这个,但是得到了正在运行的程序的pid。我正在运行一个睡眠脚本(它所做的就是睡眠),名为sleep.sh,我希望获得该脚本的pid。有没有办法做到这一点?我自己并没有找到一个很好的方法。



我也使用了ps | grep,我可以看到进程ID有没有办法输出它呢?

 进程proc1 ='ps -ef'.execute()
进程proc2 ='grep sleep.sh'。执行()
进程proc3 ='grep -v grep'.execute()
all = proc1 | proc2 | proc3

是否有办法修改all.text来获取进程ID或者是否存在另一个方式得到它?

解决方案

 对象getNumber(字符串searchProc){
//将方法调用中的进程添加到grep命令中
searchString =grep+ searchProc

//初始化命令并将它们连接在一起
Process proc1 ='ps -ef'.execute()
进程proc2 = searchString.execute()
进程proc3 ='grep -v grep'.execute()
all = proc1 | proc2 | proc3

//将输出命令的输出设置为字符串
输出= all.text

//将字符串修剪为进程ID
PID = output.substring(output.indexOf( ' '),output.size())。修剪()
PID = pid.substring(0,pid.indexOf(''))。修剪( )
return pid
}

这是我的解决方案。 (我想让它成为一种方法,所以我把方法声明放在最上面)
我的问题一开始就是在进程名和pid之间有更多的空间。但后来我发现了修剪方法,并很好地工作。如果您对我的方法有疑问,请告诉我。我会定期检查。

import java.lang.management.*

final String name = ManagementFactory.getRuntimeMXBean().getName();
final Integer pid = Integer.parseInt(name[0..name.indexOf("@")-1])

I tried this in my code but that gets the pid of the running program. I am running a sleeping script (all it does is sleep) called sleep.sh and i want to get the pid of that. Is there a way to do that? I have not found a very good way myself.

I also used a ps | grep and i can see the process id is there a way to output it though?

Process proc1 = 'ps -ef'.execute()
Process proc2 = 'grep sleep.sh'.execute()
Process proc3 = 'grep -v grep'.execute()
all = proc1 | proc2 | proc3

is there a way i can modify the all.text to get the process id or is there another way to get it?

解决方案

Object getNumber(String searchProc) {
        //adds the process in the method call to the grep command
        searchString = "grep "+searchProc

        // initializes the command and pipes them together
        Process proc1 = 'ps -ef'.execute()
        Process proc2 = searchString.execute()
        Process proc3 = 'grep -v grep'.execute()
        all = proc1 | proc2 | proc3

        //sets the output to the piped commands to a string
        output = all.text

        //trims down the string to just the process ID
        pid = output.substring(output.indexOf(' '), output.size()).trim()
        pid = pid.substring(0, pid.indexOf(' ')).trim()
        return pid
}

This is my solution. (I wanted to make it a method so i put the method declaration at the very top) My problem at the beginning was that there was more spaces than one between the process name and the pid. but then i found the trim method and that worked nicely. If you have questions on my method let me know. I will check back periodically.

这篇关于使用'command slinging'获取Groovy中另一个程序的进程ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆