如何从进程名获得进程id? [英] How to get process id from process name?

查看:120
本文介绍了如何从进程名获得进程id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个shell脚本得到的Skype应用程序的进程id 的我的Mac上。

I'm trying to create a shell script getting the process id of the Skype app on my Mac.

PS -clx | grep的Skype的| AWK'{打印$ 2}'|头-1

ps -clx | grep 'Skype' | awk '{print $2}' | head -1

以上是工作正常,但有两个问题:

The above is working fine, but there are two problems:

1)
的grep 的命令会得到所有的过程,如果他们的名字只包含Skype的。我怎样才能确保它只是得到的结果,如果进程名是完全相同的的Skype 的?

1) The grep command would get all process if their name just contains "Skype". How can I ensure that it only get the result, if the process name is exactly Skype?

2)
我想从这个做一个shell脚本,它可以从终端使用,但这个过程的名称应该是这个脚本的参数:

2) I would like to make a shell script from this, which can be used from the terminal but the process name should be an argument of this script:

#!/bin/sh

ps -clx | grep '$1' | awk '{print $2}' | head -1

这是不返回任何东西。我想这是因为的 $ 2 在AWK作为一个参数也处理。我怎样才能解决这个问题?

This isn't returning anything. I think this is because the $2 in the awk is treated as an argument too. How can I solve this?

推荐答案

PS -cl1 输出看起来是这样的:

Your ps -cl1 output looks like this:

  UID   PID  PPID        F CPU PRI NI       SZ    RSS WCHAN     S             ADDR TTY           TIME CMD
  501   185   172      104   0  31  0  2453272   1728 -      S    ffffff80145c5ec0 ??         0:00.00 httpd
  501   303     1 80004004   0  31  0  2456440   1656 -      Ss   ffffff8015131300 ??         0:11.78 launchd
  501   307   303     4004   0  33  0  2453456   7640 -      S    ffffff8015130a80 ??         0:46.17 distnoted
  501   323   303 40004004   0  33  0  2480640   9156 -      S    ffffff80145c4dc0 ??         0:03.29 UserEventAgent

因此​​,在每行中的最后一项是您的命令。这意味着你可以使用普通的前pressions的全部力量来帮助你。

Thus, the last entry in each line is your command. That means you can use the full power of regular expressions to help you.

$ 在常规的前pression意味着字符串的结束,因此,你可以使用 $ 指定不仅输出必须在它具有的Skype ,它必须与的Skype 结束。这意味着如果你有一个叫做命令 Skype的控制器,你不会拉起来:

The $ in a regular expression means the end of the string, thus, you could use $ to specify that not only does the output must have Skype in it, it must end with Skype. This means if you have a command called Skype Controller, you won't pull it up:

ps -clx | grep 'Skype$' | awk '{print $2}' | head -1

您也可以使用 PS -o 格式才刚刚拉起你想要的列简化事情:

You can also simplify things by using the ps -o format to just pull up the columns you want:

ps -eo pid,comm | grep 'Skype$' | awk '{print $1}' | head -1

和,可以消除只需使用 AWK 的能力来选择你行你。在 AWK NR 是记录号。因此,你可以这样做:

And, you can eliminate head by simply using awk's ability to select your line for you. In awk, NR is your record number. Thus you could do this:

ps -eo pid,comm | grep 'Skype$' | awk 'NR == 1 {print $1}'

哎呀,现在我想起来了,我们可以消除的grep 太:

ps -eo pid,comm | awk '/Skype$/  {print $1; exit}'

这是使用awk的使用常规的前pressions能力。如果该行包含正规前pression,Skype的$',将打印的第一列,然后退出

This is using awk's ability to use regular expressions. If the line contains the regular expression, 'Skype$', it will print the first column, then exit

唯一的问题是,如果你有一个命令美孚的Skype ,这也将它捡起来。要消除它,你将不得不做一点更看中的步法:

The only problem is that if you had a command Foo Skype, this will also pick it up. To eliminate that, you'll have to do a bit more fancy footwork:

ps -eo pid,comm | while read pid command
do
    if [[ "$command" = "Skype" ]]
    then
        echo $pid
        break
    fi
done

在读取时正在读两个变量。诀窍是,使用空格来划分它在读取变量。然而,由于只有两个变量,最后一个将包含整个行的其余部分。因此,如果命令的的Skype控制器的,整个命令将被放入 $命令即使在它的空间。

The while read is reading two variables. The trick is that read uses white space to divide the variables it reads in. However, since there are only two variables, the last one will contain the rest of the entire line. Thus if the command is Skype Controller, the entire command will be put into $command even though there's a space in it.

现在,我们没有使用常规的前pression。我们可以用一个比较平等的命令。

Now, we don't have to use a regular expression. We can compare the command with an equality.

这是长在打字,但你实际使用更少的命令,少管。记住 AWK 通过每个行循环。所有你在这里所做的是使它更加明确。最后,其实这是非常有效的,你原来有什么。

This is longer to type in, but you're actually using fewer commands and less piping. Remember awk is looping through each line. All you're doing here is making it more explicit. In the end, this is actually much more efficient that what you originally had.

这篇关于如何从进程名获得进程id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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