pidof 与pgrep 区别
1、pidof用法
pidof 是Linux系统中用来查找正在运行进程的进程号(pid)的工具;
语法
pidof(选项)(参数)
选项
-s:仅返回一个进程号;
-c:仅显示具有相同“root”目录的进程;
-x:显示由脚本开启的进程;
-o:指定不显示的进程ID。
参数
进程名称:指定要查找的进程名称。
实例:
# pidof nginx
28895 28078 28077 28076 28075
# pidof -s nginx
28895
2、pgrep用法
经常要查看进程的信息,包括进程的是否已经消亡,通过pgrep来获得正在被调度的进程的相关信息。pgrep通过匹配其程序名,找到匹配的进程
选项
-l 同时显示进程名和PID
-o 当匹配多个进程时,显示进程号最小的那个
-n 当匹配多个进程时,显示进程号最大的那个
注:进程号越大,并不一定意味着进程的启动时间越晚
实例:
1.查看指定名称的进程信息
(默认只显示PID)
# pgrep ssh
873
121387
3.2.同时显示PID和ProcessName : –l
# pgrep -l ssh
873 sshd
121387 sshd
3.-o 当匹配多个进程时,显示进程号最小的那个
# pgrep -l -o ssh
873 sshd
4.-n 当匹配多个进程时,显示进程号最大的那个
# pgrep -l -n ssh
121387 sshd
特别说明
pgrep 相当于 ps -eo pid,cmd | awk ‘{print $1,$2}’ |grep sshd
# ps -eo pid,cmd | awk '{print $1,$2}' |grep sshd
873 /usr/sbin/sshd