Pidof找不到这个过程

时间:2015-01-23 10:07:00

标签: linux shell

我想使用pidof或ps命令或任何。

找出shell脚本进程ID

我想要的只是它的进程ID。我使用了' pidof -x test.sh'。哪个不行。注意:我不想调用/ bin / sh或/ bin / bash - 因为脚本不起作用。如果我在脚本中调用/ bin / sh,pidof正在工作。

请帮忙

4 个答案:

答案 0 :(得分:6)

pgrep -f脚本给出了预期的结果。 感谢

答案 1 :(得分:1)

ps -ef | grep your_search_string | awk {printf $2}

答案 2 :(得分:0)

pidof 和 pgrep 都是为某个进程查找 pid 的选项。不要运行 ps -ef | grep "your_command" 因为你现在也用 grep 匹配污染了你的结果。

  • 使用 pidof -s [program] 查找父进程 ID。
  • 使用 pidof [program] 查找所有 pid(即 python 在您的系统上运行多次)。
  • 使用 pgrep [program] 匹配由 python 运行的二进制文件的名称(因为 pidof 不太适合此用例)。

Fox 很好地解释了 pidof 与 pgrep 之间的区别。看他的answer

为方便起见,我将其复制到此处:

<块引用>

程序 pgrep 和 pidof 不是一回事,但它们 非常相似。例如:

$ pidof 'firefox'
5696
$ pgrep '[i]ref'
5696
$ pidof '[i]ref'
$ printf '%s\n' "$?"
1

如您所见,pidof 未能找到 [i]ref 的匹配项。这是 因为 pidof 程序返回所有关联进程 ID 的列表 用一个叫做程序的程序。另一方面, pgrep re 返回一个 与名称匹配的程序关联的所有进程 ID 的列表 正则表达式 re.

在最基本的形式中,等价实际上是:

$ pidof 'program'
$ pgrep '^program$'

再举一个具体的例子,考虑:

$ ps ax | grep '[w]atch'
   12 ?        S      0:04 [watchdog/0]
   15 ?        S      0:04 [watchdog/1]
   33 ?        S<     0:00 [watchdogd]
18451 pts/5    S+     0:02 watch -n600 tail log-file
$ pgrep watch
12
15
33
18451
$ pidof watch
18451

答案 3 :(得分:0)

对于非脚本进程的另一个警告:

pidof 忽略:

  • 僵尸进程
  • 磁盘休眠中的进程

因此 pidof 以及 killall(相同的代码库)与 pgrep 不同,只要它在磁盘 i/o 中被阻塞,就不会看到您的进程。

我刚刚在 pidof 中遇到了这种情况 - 找到,未找到,找到,...