⚪介绍

若是某个进程执行一半需要停止时,或是已消耗了很大的系统资源时,此时可以考虑停止该进程。使用kill命令来完成此项任务。

⚪基本语法

kill  [选项] 进程号(功能描述:通过进程号来杀死进程)

killall 进程名称  (功能描述:通过进程名称杀死进程,同时也干掉子进程,也支持通配符,这在系统负载过大而变得很慢时很有用)

⚪常用选项

       -9:表示强迫进程立即停止

⚪使用案例

案例1:踢掉非法登录用户tom

首先ps -aux | grep sshd 查询非法用户tom的pid

[root@localhost131 ~]# ps -aux | grep sshd
root       7541  0.0  0.2 112756  4348 ?        Ss   10:55   0:00 /usr/sbin/sshd -D
root      10566  0.0  0.2 156636  5500 ?        Rs   15:05   0:00 sshd: root@pts/0
root      12127  1.1  0.2 156636  5436 ?        Ss   17:43   0:00 sshd: tom [priv]
tom       12131  0.0  0.1 156636  2416 ?        S    17:43   0:00 sshd: tom@pts/1
root      12179  0.0  0.0 112728   984 pts/0    S+   17:43   0:00 grep --color=auto sshd

然后再用kill 12127踢掉tom的登录

案例2:终止远程登录服务sshd,在适当时候在重启sshd服务

       先用ps -aux | grep sshd 找到/usr/sbin/sshd -D 进程号7541

       然后kill 7541 杀掉进程,关闭sshd服务

       重新开启sshd服务/bin/systemctl sshd start 或者 /bin/systemctl start sshd.service

案例3:终止多个gedit,演示killall

       killall gedit 杀掉所有开启的gedit文本编辑器

案例4:强制杀掉一个终端

       ps -aux | grep bash 查询终端bash的PID

       kill -9  bash对应的PID