Linux pkill command

作用

pkill 用做管理进程时,pkill 命令和 killall 命令的用法相同,都是通过进程名杀死一类进程
pkill 另一重要的功能就是按照终端用户来剔出用户登录

用法

  1. 语法格式

    1
    pkill [options] pattern
  2. 参数选项

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    -<sig>, --signal <sig>    signal to send (either number or name)
    -e, --echo display what is killed
    -c, --count count of matching processes
    -f, --full use full process name to match
    -g, --pgroup <PGID,...> match listed process group IDs
    -G, --group <GID,...> match real group IDs
    -n, --newest select most recently started
    -o, --oldest select least recently started
    -P, --parent <PPID,...> match only child processes of the given parent
    -s, --session <SID,...> match session IDs
    -t, --terminal <tty,...> match by controlling terminal
    -u, --euid <ID,...> match by effective IDs
    -U, --uid <ID,...> match by real IDs
    -x, --exact match exactly with the command name
    -F, --pidfile <file> read PIDs from file
    -L, --logpidfile fail if PID file is not locked
    --ns <PID> match the processes that belong to the same
    namespace as <pid>
    --nslist <ns,...> list which namespaces will be considered for
    the --ns option.
    Available namespaces: ipc, mnt, net, pid, user, uts

    -h, --help display this help and exit
    -V, --version output version information and exi
  3. 用法示例

    用做进程管理,根据进程名杀死一类进程

    语法格式

    1
    pkill [信号] 进程名

    命令常用的信号及其含义

    信号编号 信号名 含义
    0 EXIT 程序退出时收到该信息。
    1 HUP 挂掉电话线或终端连接的挂起信号,这个信号也会造成某些进程在没有终止的情况下重新初始化。
    2 INT 表示结束进程,但并不是强制性的,常用的 “Ctrl+C” 组合键发出就是一个 kill -2 的信号。
    3 QUIT 退出。
    9 KILL 杀死进程,即强制结束进程。
    11 SEGV 段错误。
    15 TERM 正常结束进程,是 kill 命令的默认信号。

    用法示例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    # 查看python进程
    [root@hadoop-centos-01 ~]# pstree -p | grep python
    | |-sshd(33793)---bash(33803)---python(34176)
    [root@hadoop-centos-01 ~]#
    [root@hadoop-centos-01 ~]# ps -ef | grep python
    root 34176 33803 0 22:30 pts/2 00:00:00 python
    root 34188 33542 0 22:31 pts/1 00:00:00 grep --color=auto python

    # 执行pkill后再去查看,python进程已被杀死
    [root@hadoop-centos-01 ~]# pkill -kill python
    [root@hadoop-centos-01 ~]# pstree -p | grep python
    [root@hadoop-centos-01 ~]# ps -ef | grep python
    root 34241 33542 0 22:33 pts/1 00:00:00 grep --color=auto python

    踢出登陆用户

    语法格式

    1
    pkill [-t 终端号] 进程名

    用法示例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    [root@hadoop-centos-01 ~]# w # 查看当前登陆的用户
    22:47:54 up 1 day, 16:20, 4 users, load average: 0.00, 0.01, 0.05
    USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
    root pts/0 192.168.244.6 08:03 14:43m 0.13s 0.13s -bash
    root pts/1 192.168.244.6 21:56 2.00s 0.41s 0.01s w
    root pts/2 192.168.244.6 22:03 18.00s 0.10s 0.10s -bash
    root pts/3 192.168.244.6 06:35 14:43m 0.17s 0.17s -bash

    [root@hadoop-centos-01 ~]#
    [root@hadoop-centos-01 ~]# pkill -kill -t pts/2
    [root@hadoop-centos-01 ~]# w # 再次查看,pts/2已被杀掉
    22:51:26 up 1 day, 16:24, 3 users, load average: 0.01, 0.02, 0.05
    USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
    root pts/0 192.168.244.6 08:03 14:46m 0.13s 0.13s -bash
    root pts/1 192.168.244.6 21:56 6.00s 0.43s 0.02s w
    root pts/3 192.168.244.6 06:35 14:47m 0.17s 0.17s -bash
-------------本文结束感谢您的阅读-------------