linux pidof

本文介绍了 Linux 下 pidof 命令的用法及其参数,通过实例展示了如何查找运行中程序的进程 ID,并对比了 pgrep 命令。此外还提到了与 killall 和 xargs 结合使用的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

转载:http://blog.51osos.com/linux/linux-pidof-command/

什么是pidof命令?
#man pidof中的解释:
pidof — find the process ID of a running program.
pidof–用于查找一个运行的程序的PID。
pidof is actually the same program as killall5;
[root@GoGo ~]# ls -l /sbin/pidof
lrwxrwxrwx. 1 root root 8 Aug 25 00:40 /sbin/pidof -> killall5 //的确如此。

参数:
-s 表示只返回1个 pid
-x 表示同时返回运行给定程序的 shell 的 pid
-o 表示告诉 piod 表示忽略后面给定的 pid ,可以使用多个 -o 。
pidof is simply a (symbolic) link to the killall5 program, which should also be located in /sbin.
When pidof is invoked with a full pathname to the program it should find the pid of, it is reasonably safe. Otherwise it
is possible that it returns pids of running programs that happen to have the same name as the program you’re after but are
actually other programs.
如果 是一个 fullpath 则 pidof 不会出错,但如果不是则有可能返回和给定同名的程序。

还是看实例吧。

[root@GoGo ~]# pidof nginx

1476 1475 1473 1472 1471 1470 1469 1467 1466

[root@GoGo ~]# ps -ef | grep nginx

nginx     1450  1449  0 20:16 ?        00:00:00 php-fpm: pool www

nginx     1451  1449  0 20:16 ?        00:00:00 php-fpm: pool www

nginx     1452  1449  0 20:16 ?        00:00:00 php-fpm: pool www

nginx     1453  1449  0 20:16 ?        00:00:00 php-fpm: pool www

nginx     1454  1449  0 20:16 ?        00:00:00 php-fpm: pool www

root      1466     1  0 20:16 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf

nginx     1467  1466  0 20:16 ?        00:00:00 nginx: worker process

nginx     1469  1466  0 20:16 ?        00:00:00 nginx: worker process

nginx     1470  1466  0 20:16 ?        00:00:00 nginx: worker process

nginx     1471  1466  0 20:16 ?        00:00:00 nginx: worker process

nginx     1472  1466  0 20:16 ?        00:00:00 nginx: worker process

nginx     1473  1466  0 20:16 ?        00:00:00 nginx: worker process

nginx     1475  1466  0 20:16 ?        00:00:00 nginx: worker process

nginx     1476  1466  0 20:16 ?        00:00:00 nginx: worker process

root      2234  2141  0 20:24 pts/1    00:00:00 grep nginx

[root@GoGo ~]# ps -ef | grep nginx | awk ’{print $2}’

1450

1451

1452

1453

1454

1466

1467

1469

1470

1471

1472

1473

1475

1476

2237

[root@GoGo ~]# pidof -s nginx

1476

[root@GoGo ~]# pidof sshd

2139 1163

[root@GoGo ~]# ps -ef | grep sshd

root      1163     1  0 20:16 ?        00:00:00 /usr/sbin/sshd

root      2139  1163  0 20:20 ?        00:00:00 sshd: root@pts/1

延伸:

pgrep命令可以达到同样的效果,本主不做介绍,51开源社区之前曾详细介绍。

linux pgrep命令详解:http://bbs.51osos.com/thread-3936-1-1.html

另个pidof可以结合xargs kill 使用,如

pidof xxx | xargs kill 当然有时也可以使用killall代替。不过对于nginx可不要这样操作噢,最好还是平滑关闭。

转载于:https://www.cnblogs.com/yunsicai/p/3675938.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值