Pidof 找不到进程

Pidof not finding the process

我想使用 pidof 或 ps 命令或任何命令找出 shell 脚本进程 ID。

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

请帮忙

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

ps -ef | grep your_search_string | awk {printf }

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

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

Fox explained the difference between pidof vs pgrep quite well. see his answer

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

The programs pgrep and pidof are not quite the same thing, but they are very similar. For example:

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

As you can see, pidof failed to find a match for [i]ref. This is because pidof program returns a list of all process IDs associated with a program called program. On the other hand, pgrep re returns a list of all process IDs associated with a program whose name matches the regular expression re.

In their most basic forms, the equivalence is actually:

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

As yet another concrete example, consider:

$ 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

即使是非脚本进程的另一个警告:

pidof 忽略:

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

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

我刚刚使用 pidof 遇到了这个问题 - 找到,没有找到,找到,...