小编Pig*_*ras的帖子

为什么 pidof 和 pgrep 的行为不同?

我有一个 init 脚本/etc/init.d/myservice用于初始化这样的服务:

...
start() {
  ...
  daemon /usr/sbin/myservice
  ...
}

stop() {
  ...
  pgrep myservice
  pidof myservice
  ps -ef | grep myservice
  ...
}
Run Code Online (Sandbox Code Playgroud)

当我尝试停止服务时,这是输出:

10000 10001
10000
root      10000     1  0 09:52 ?        00:00:02 /usr/sbin/myservice
root      9791   9788  0 10:06 pts/1    00:00:00 /bin/sh /sbin/service myservice stop
root      10001  9791  1 10:06 pts/1    00:00:00 /bin/sh /etc/init.d/myservice stop 
root      9805   9796  0 10:06 pts/1    00:00:00 grep myservice
Run Code Online (Sandbox Code Playgroud)

这是预期的吗?为什么pidof只返回我想要停止的服务的正确 PID 并pgrep返回服务 PID 和 init 脚本的 …

grep process init-script pidof

9
推荐指数
1
解决办法
1万
查看次数

标签 统计

grep ×1

init-script ×1

pidof ×1

process ×1