Linux ps grep kill
2010-12-27 13:47:38 阿炯

想结束系统中指定的进程,有以下指令可能参考:

ps -ef | grep pure-ftpd | grep -v grep | awk '{print $2" "$3}' | xargs kill -9 = pkill pure-ftpd

killall -9 nginx 或者 kill -9 `ps aux |grep -i nginx |grep -v grep |awk '{print $2}'` = kill -9 `pgrep nginx`

以上是很常规的方法,下面我们介绍一种系统自带的工具的用法。

名称
pgrep, pkill(其实是pgrep的链接) – 查找或者发信号给进程通过名称和其它属性,我们能得到进程许多信息和通过这些来对其进行相关操作。

语法
pgrep [-flvx] [-n | -o] [-d delim] [-P ppidlist] [-g pgrplist] [-s sidlist] [-u euidlist] [-U uidlist] [-G gidlist] [-t termlist] [-c ctidlist] [-z zoneidlist] [pattern]

pkill [-signal] [-fvx] [-n | -o] [-P ppidlist] [-g pgrplist] [-s sidlist] [-u euidlist] [-U uidlist] [-G gidlist] [-J projidlist] [-t termlist] [-T taskidlist] [-c ctidlist] [-z zoneidlist] [pattern]

说明:
pgrep程序检查在系统的中活动进程,报告进程属性匹配命令行上指定条件的进程的ID。每一个进程ID以一个十进制数表示,通过一个分割字符串和下一个ID分开,默认的分割字符串是一个新行。对于每个属性选项,用户可以在命令行上指定一个以逗号分割的可能值的集合。

例如:
pgrep -G other,daemon
匹配真实组ID是other或者是daemon的进程。如果多个条件被指派,这些匹配条件按逻辑与"AND"规则运算。

例如:
pgrep -G other,daemon -U root,daemon
匹配进程它的属性是:
(真实的组ID是other或者是daemon) 与 (真实的用户ID是root或者是daemon)。

pkill函数和pgrep相同,除了匹配的进程被信号通知就像用kill(1)替代了pgrep的进程ID的输出,一个信号名称或者数字可以作为pkill的第一个命令行选项。

选项
支持下面的选项:
-c ctidlist  仅匹配列表中列出的ID的进程数。
# pgrep -c ssh -l
6

-d delim  指定每一个匹配的进程ID之间分割字符串,如果没有 -d 选项指定,默认的是新行字符,-d 选项仅在pgrep命令中有效。

-f 正则表达式模式将执行与完全进程参数字符串 (从/proc/nnnnn/psinfo文件的pr_psargs字段获得)匹配。如果没有 -f 选项,表达式仅对执行文件名称(从/proc/nnnnn/psinfo文件pr_fname字段获得)匹配。
# pgrep -f ng -l
 2159 nginx: master process /usr/sbin/nginx
 2160 nginx: worker process
 2222 hald-addon-input: Listening on /dev/input/event3 /dev/input/event2 /dev/input/event0
 2230 hald-addon-acpi: listening on acpid socket /var/run/acpid.socket
 2231 hald-addon-storage: polling /dev/hdc (every 2 sec)

-g pgrplist 仅匹配进程组ID在给定列表中的进程,如果组0包括在列表中,这个被解释为pgrep或者pkill进程的组ID。
# pgrep -g 2159 -l
 2159 nginx(所有nginx进程的父进程)
 2160 nginx

-G gidlist 仅匹配真实组ID在给定列表中的进程,每一个组ID可以使用组名称或者数字的组ID指定。
# pgrep -G 1000 -l
 2388 sshd
 2395 bash
 2420 sshd
 2421 sftp-server

-l 长格式输出,输出每一个匹配进程的名称连同进程ID,进程名称从pr_psargs 或者 pr_fname字段获得,依赖于-f选项是否指定。-l选项仅在pgrep命令中有效。

-n 匹配最新(最近生成的)符合所有其它匹配条件的进程,不能和-o选项一起使用。

-o 匹配最旧(最早生成的)符合所有其它匹配条件的进程,不能和-n选项一起使用。

-P ppidlist 仅匹配给定列表中父进程ID的进程。
# pgrep -P 2159 -l
 2160 nginx

-s sidlist 仅匹配进程会话ID在给定列表中的进程,如果ID 0在列表中,这个解释为pgrep或者pikill进程的会话ID。
# pgrep -s 2159 -l
 2159 nginx
 2160 nginx

-t termlist 仅匹配与给定列表中终端关联的进程,每一个终端指定为在/dev中终端设备路径名称的后缀。例如term/a 或者 pts/0。
# pgrep -t pts/1 -l
 2395 bash

-u euidlist 仅匹配有效用户ID在给定列表中的进程,每个用户ID可以通过一个登录名称或者数字的用户ID指定。
# pgrep -u hto -l
 2388 sshd
 2395 bash

-U uidlist 仅匹配真实的用户ID在给定列表中的进程,每个用户ID可以通过一个登录名称或者数字的用户ID指定。
# pgrep -U hto -l
 2388 sshd
 2395 bash
 2420 sshd
 2421 sftp-server

-v 反向匹配。匹配所有的进程除了符合匹配条件的。

-x 仅认为进程的参数字符串或者执行文件名称正确匹配规定模式是匹配的进程,模式被认为是准确的当所有在进程参数字符串或者可执行文件名称的字符匹配模式。
# pgrep -d, -x nginx
 2159,2160

-z zoneidlist 仅匹配区域ID在给定列表中的进程,每一个区域ID可以使用一个区域名称或者一个数字的区域ID指定,这个选项仅在全局区域中执行有效,如果pkill程序用来往其它区域的进程发信号,进城必须宣称{PRIV_PROC_ZONE}特权。

-signal 指定发往每一个匹配进程的信号。如果没有指定,SIGTERM 是默认的信号。-signal仅在pkill命令中作为第一个选项有效。信号可以是在signal.h中定义的没有SIG前缀的一个符号名字,也可是一个相应的信号数值。

操作

支持如下操作:
pattern
指定一个扩展正则表达式(Extended Regular Expression (ERE))模式来匹配可执行文件名称或者完整的进程参数字符串,ERE语法的完整描述参看regex(5)。

示例

例1、获得一个进程的ID号
获得nginx的进程ID:
$ pgrep -x -u root nginx
2123

例2、终止一个进程
终止最近生成的apache2:
$ pkill -n apache2

Example 3: Find the process ID of the named daemon:
$ pgrep -u root named

Example 4: Make syslog reread its configuration file:
$ pkill -HUP syslogd

Example 5: Give detailed information on all xterm processes:
$ ps -fp $(pgrep -d, -x xterm)

Exit Status
如下退出值被返回:
0 一个或者多个进程被匹配。
1 没有进程被匹配。
2 指定了无效的命令行参数。
3 出现一个致命的错误。

将用户运行的进程找出并通过进程ID来终结它们
find out all running processes of user account and kill them by determine the PIDs (Process IDs) of processes owned by the user using:
# pgrep -u freeoa
# ps -f --pid $(pgrep -u freeoa)

you can use the killall command to kill those running processes as shown.

# killall -9 -u freeoa

The -9 is the signal number for the SIGKILL signal or use -KILL instead of -9 and -u defines username.

Note: In recent releases of RedHat/CentOS 7.x versions and Fedora 21+, you will get error message as:
-bash: killall: command not found

To fix such error, you need to install psmisc package as shown:
# yum install psmisc [On RedHat/CentOS 7.x]

Package: psmisc
Description-en: utilities that use the proc file system
This package contains miscellaneous utilities that use the proc FS:
  - fuser: identifies processes that are using files or sockets.
  - killall: kills processes by name (e.g. "killall -HUP named").
  - peekfd: shows the data traveling over a file descriptor.
  - pstree: shows currently running processes as a tree.
  - prtstat: print the contents of /proc/<pid>/stat

Homepage: http://psmisc.sourceforge.net/



pkill命令用法之 - 从终端踢掉其他登录者


想从终端快速踢掉系统中其他登录者,可以先用w命令查看当前所有登录信息,并确定自己所在终端号。命令显示中有'w'的行所指明的TTY值就是你当前登录的终端号(pts/1),接下来用pkill命令按需求踢掉其他的登陆者即可,假如现在你想踢掉当前登录用户gentoo,从上面的w命令看gentoo用户所在的终端号为(pts/0),使用如下pkill命令:
# pkill -9 -t pts/0

再用w命令查看

命令格式:
pkill -signal -t SIGNAL
-signal
--signal signal

Defines the signal to send to each matched process.  Either the numeric or the symbolic signal name can be used.  (pkill only.)

-t, --terminal term,...

Only match processes whose controlling terminal is listed.  The terminal name should be specified without the "/dev/" prefix.

SIGNAL

Signal  Value  ActionComment

SIGHUP  1 Term Hangup detected on controlling terminal or death of controlling process

SIGINT  2 Term Interrupt from keyboard

SIGQUIT 3 Core Quit from keyboard

SIGILL  4 Core Illegal Instruction

SIGABRT 6 Core Abort signal from abort(3)

SIGFPE  8 Core Floating point exception

SIGKILL 9 Term Kill signal

SIGSEGV    11 Core Invalid memory reference

SIGPIPE    13 Term Broken pipe: write to pipe with no readers

SIGALRM    14 Term Timer signal from alarm(2)

SIGTERM    15 Term Termination signal

SIGUSR    130,10,16 Term User-defined signal 1

SIGUSR    231,12,17 Term User-defined signal 2

SIGCHLD    20,17,18 Ign  Child stopped or terminated

SIGCONT    19,18,25 Cont Continue if stopped

SIGSTOP    17,19,23 Stop Stop process

SIGTSTP    18,20,24 Stop Stop typed at terminal

SIGTTIN        21,21,26 Stop Terminal input for background process

SIGTTOU    22,22,27 Stop Terminal output for background process