pidof 用於查找正在運行程序的進程 ID (PID)。實際是 killall5 的符號鏈接:
ls -l /sbin/pidof
# lrwxrwxrwx 1 root root 8 Aug 25 00:40 /sbin/pidof -> killall5
| 參數 | 說明 |
|---|---|
-s | 僅返回一個 PID (通常為主進程) |
-x | 同時返回腳本/Shell 進程 PID |
-o PID | 忽略指定 PID (可多次使用) |
[root@GoGo ~]# pidof nginx
1476 1475 1473 1472 1471 1470 1469 1467 1466
對比 ps -ef | grep nginx 可見包含 master/worker 進程。
[root@GoGo ~]# pidof -s nginx
1476
[root@GoGo ~]# pidof sshd
2139 1163
對比 ps -ef | grep sshd:1163 為主進程,2139 為當前 SSH 會話子進程。
pidof /usr/sbin/nginx。-s 通常返回第一個找到的。kill 使用:kill $(pidof nginx) 批量發送信號。| 命令 | 優點 | 缺點 |
|---|---|---|
| pidof | 極簡、輸出純 PID 列表、腳本友好 | 不支持正則、用戶過濾、父進程樹 |
| pgrep | 支持 -u 用戶、-P 父進程、-f 完整命令行正則 | 輸出格式固定 |
| ps + grep | 信息最全 (用戶、CPU、內存、啟動時間) | 需額外解析、grep 進程自身干擾 |