linux查看服务pid
pgrep是一个命令行实用程序,根据给定的条件查找正在运行的程序的进程id。它可以是完整的或部分的进程名、运行该进程的用户或其他属性。
语法:
pgrep [options] pattern
当在没有任何选项的情况下调用时,pgrep将显示与给定名称匹配的所有正在运行的程序的pid。例如,要找到SSH服务器的PID,可以运行以下命令:
pgrep http
如果想要结束http进程,可以使用pkill命令
[root@localhost ~]# pkill ssh
pgrep在换行中打印每个匹配的进程ID。-d选项允许指定不同的分隔符。例如,如果想使用空格作为分隔符,运行下面的命令:
[root@localhost ~]# pgrep http -l -d ’ ‘
使用-l选项可以列出PID和进程名称:
[root@localhost ~]# pgrep -l http
如果想完全匹配,可以使用:
[root@localhost ~]# pgrep ‘^httpd$’ -l
使用-u选项告诉pgrep显示给定用户正在运行的进程:
[root@localhost ~]# pgrep -u root -l
[root@localhost ~]# pgrep -v -u root -l
[root@localhost ~]# pgrep -v -u root -l -c