kill

kill

进程管理

向进程发送控制信号,以实现对进程管理,每个信号对应一个数字,
信号名称以SIG开头(可省略),不区分大小写
显示当前系统可用信号: kill –l,trap -l

[root@centos ~]#kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

常用信号:man 7 signal

  1. SIGHUP: 无须关闭进程而让其重读配置文件
  2. SIGINT: 中止正在运行的进程;相当于Ctrl+c
  3. SIGQUIT:相当于ctrl+\
  4. SIGKILL: 强制杀死正在运行的进程
  5. SIGTERM:终止正在运行的进程
  6. SIGCONT:继续运行
  7. SIGSTOP:后台休眠 指定信号的方法:
    (1) 信号的数字标识:1, 2, 9
    (2) 信号完整名称:SIGHUP
    (3) 信号的简写名称:HUP

按PID:kill [-SIGNAL] pid …
kill –n SIGNAL pid
kill –s SIGNAL pid
按名称:killall [-SIGNAL] comm…
按模式:pkill [options] pattern

-SIGNAL
-u uid: effective user,生效者 
-U uid: real user,真正发起运行命令者 
-t terminal: 与指定终端相关的进程
-l: 显示进程名(pgrep可用)
-a: 显示完整格式的进程名(pgrep可用) 
-P pid: 显示指定进程的子进程

注意:
kill -9 1 很危险,表面上没什么 影响,实则会影响再生进程
再生进程:自身进程被杀后,换个 进程编号继续运行(一般如终端)

Linux的作业控制

前台作业:通过终端启动,且启动后一直占据终端;
后台作业:可通过终端启动,但启动后即转入后台运行(释放终端)

让作业运行于后台
(1) 运行中的作业: Ctrl+z
(2) 尚未启动的作业:# COMMAND &
后台作业虽然被送往后台运行,但其依然与终端相关;退出终端,将关闭 后台作业。
如果希望送往后台后,剥离与终端的关系
# nohup COMMAND &> /dev/null
#screen;COMMAND

[root@centos ~]#screen
[root@centos ~]#ping 0
PING 0 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.098 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.051 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.053 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.103 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.059 ms
64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.048 ms
64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.049 ms
64 bytes from 127.0.0.1: icmp_seq=8 ttl=64 time=0.050 ms
64 bytes from 127.0.0.1: icmp_seq=9 ttl=64 time=0.050 ms
64 bytes from 127.0.0.1: icmp_seq=10 ttl=64 time=0.053 ms
#关掉终端,重新开启终端
[root@centos ~]#screen -ls
There is a screen on:
        5069.pts-1.centos       (Detached)
1 Socket in /var/run/screen/S-root.

[root@centos ~]#screen -r
64 bytes from 127.0.0.1: icmp_seq=10 ttl=64 time=0.081 ms
64 bytes from 127.0.0.1: icmp_seq=11 ttl=64 time=0.049 ms
64 bytes from 127.0.0.1: icmp_seq=12 ttl=64 time=0.049 ms
64 bytes from 127.0.0.1: icmp_seq=13 ttl=64 time=0.049 ms
64 bytes from 127.0.0.1: icmp_seq=14 ttl=64 time=0.053 ms
64 bytes from 127.0.0.1: icmp_seq=15 ttl=64 time=0.034 ms
64 bytes from 127.0.0.1: icmp_seq=16 ttl=64 time=0.048 ms
64 bytes from 127.0.0.1: icmp_seq=17 ttl=64 time=0.050 ms
64 bytes from 127.0.0.1: icmp_seq=18 ttl=64 time=0.051 ms
64 bytes from 127.0.0.1: icmp_seq=19 ttl=64 time=0.051 ms
64 bytes from 127.0.0.1: icmp_seq=20 ttl=64 time=0.052 ms
64 bytes from 127.0.0.1: icmp_seq=21 ttl=64 time=0.050 ms
64 bytes from 127.0.0.1: icmp_seq=22 ttl=64 time=0.051 ms
64 bytes from 127.0.0.1: icmp_seq=23 ttl=64 time=0.053 ms
64 bytes from 127.0.0.1: icmp_seq=24 ttl=64 time=0.052 ms
64 bytes from 127.0.0.1: icmp_seq=25 ttl=64 time=0.052 ms
64 bytes from 127.0.0.1: icmp_seq=26 ttl=64 time=0.050 ms
64 bytes from 127.0.0.1: icmp_seq=27 ttl=64 time=0.053 ms
^C
--- 0 ping statistics ---
27 packets transmitted, 27 received, 0% packet loss, time 26001ms
rtt min/avg/max/mdev = 0.034/0.074/0.672/0.117 ms

查看当前终端所有作业:
# jobs
作业控制:
# fg [[%]JOB_NUM]:把指定的后台作业调回前台
# bg [[%]JOB_NUM]:让送往后台的作业在后台继续运行 # kill [%JOB_NUM]: 终止指定的作业

kill_第1张图片
[root@centos ~]#ping 0 &
[1] 4477
[root@centos ~]#PING 0 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.044 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.051 ms
ls64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.052 ms

anaconda-ks.cfg  Documents  httpd-2.4.25          motd.txt  SC
b                Downloads  httpd-2.4.25.tar.bz2  Music     Templates
c                ff         initial-setup-ks.cfg  Pictures  Videos
Desktop          hello      jiaoben.sh            Public
[root@centos ~]#64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.050 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.049 ms
64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.050 ms
fg 64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.052 ms
1
ping 0
64 bytes from 127.0.0.1: icmp_seq=8 ttl=64 time=0.052 ms
64 bytes from 127.0.0.1: icmp_seq=9 ttl=64 time=0.052 ms
ls64 bytes from 127.0.0.1: icmp_seq=10 ttl=64 time=0.050 ms

64 bytes from 127.0.0.1: icmp_seq=11 ttl=64 time=0.054 ms
64 bytes from 127.0.0.1: icmp_seq=12 ttl=64 time=0.051 ms
64 bytes from 127.0.0.1: icmp_seq=13 ttl=64 time=0.052 ms
64 bytes from 127.0.0.1: icmp_seq=14 ttl=64 time=0.047 ms
^Z
[1]+  Stopped                 ping 0
[root@centos ~]#jobs
[1]+  Stopped                 ping 0
[root@centos ~]#pidof ping
4477
[root@centos ~]#fg
ping 0
64 bytes from 127.0.0.1: icmp_seq=15 ttl=64 time=0.042 ms
64 bytes from 127.0.0.1: icmp_seq=16 ttl=64 time=0.050 ms
k64 bytes from 127.0.0.1: icmp_seq=17 ttl=64 time=0.052 ms
ill 64 bytes from 127.0.0.1: icmp_seq=18 ttl=64 time=0.051 ms
-64 bytes from 127.0.0.1: icmp_seq=19 ttl=64 time=0.049 ms
1964 bytes from 127.0.0.1: icmp_seq=20 ttl=64 time=0.051 ms
 64 bytes from 127.0.0.1: icmp_seq=21 ttl=64 time=0.052 ms
4464 bytes from 127.0.0.1: icmp_seq=22 ttl=64 time=0.051 ms
764 bytes from 127.0.0.1: icmp_seq=23 ttl=64 time=0.050 ms
7
64 bytes from 127.0.0.1: icmp_seq=24 ttl=64 time=0.049 ms
64 bytes from 127.0.0.1: icmp_seq=25 ttl=64 time=0.055 ms
64 bytes from 127.0.0.1: icmp_seq=26 ttl=64 time=0.047 ms
64 bytes from 127.0.0.1: icmp_seq=27 ttl=64 time=0.050 ms
64 bytes from 127.0.0.1: icmp_seq=28 ttl=64 time=0.050 ms
l64 bytes from 127.0.0.1: icmp_seq=29 ttl=64 time=0.048 ms
s
64 bytes from 127.0.0.1: icmp_seq=30 ttl=64 time=0.040 ms
64 bytes from 127.0.0.1: icmp_seq=31 ttl=64 time=0.061 ms
64 bytes from 127.0.0.1: icmp_seq=32 ttl=64 time=0.050 ms
64 bytes from 127.0.0.1: icmp_seq=33 ttl=64 time=0.051 ms
ls64 bytes from 127.0.0.1: icmp_seq=34 ttl=64 time=0.040 ms

64 bytes from 127.0.0.1: icmp_seq=35 ttl=64 time=0.049 ms
64 bytes from 127.0.0.1: icmp_seq=36 ttl=64 time=0.050 ms
ki64 bytes from 127.0.0.1: icmp_seq=37 ttl=64 time=0.052 ms
ll 64 bytes from 127.0.0.1: icmp_seq=38 ttl=64 time=0.058 ms
-64 bytes from 127.0.0.1: icmp_seq=39 ttl=64 time=0.059 ms
1964 bytes from 127.0.0.1: icmp_seq=40 ttl=64 time=0.050 ms
 64 bytes from 127.0.0.1: icmp_seq=41 ttl=64 time=0.047 ms
64 bytes from 127.0.0.1: icmp_seq=42 ttl=64 time=0.048 ms
64 bytes from 127.0.0.1: icmp_seq=43 ttl=64 time=0.051 ms
 44764 bytes from 127.0.0.1: icmp_seq=44 ttl=64 time=0.044 ms
7
64 bytes from 127.0.0.1: icmp_seq=45 ttl=64 time=0.045 ms
64 bytes from 127.0.0.1: icmp_seq=46 ttl=64 time=0.050 ms
^C
--- 0 ping statistics ---
46 packets transmitted, 46 received, 0% packet loss, time 87637ms
rtt min/avg/max/mdev = 0.040/0.049/0.061/0.010 ms
[root@centos ~]#ping 0 &  
[1] 4500
[root@centos ~]#PING 0 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.090 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.052 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.051 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.049 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.052 ms
f64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.048 ms
g
ping 0
64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.061 ms
64 bytes from 127.0.0.1: icmp_seq=8 ttl=64 time=0.051 ms
64 bytes from 127.0.0.1: icmp_seq=9 ttl=64 time=0.049 ms
64 bytes from 127.0.0.1: icmp_seq=10 ttl=64 time=0.050 ms
64 bytes from 127.0.0.1: icmp_seq=11 ttl=64 time=0.049 ms
^C
--- 0 ping statistics ---
11 packets transmitted, 11 received, 0% packet loss, time 10001ms
rtt min/avg/max/mdev = 0.048/0.054/0.090/0.014 ms
[root@centos ~]#ping 0 &
[1] 4501
[root@centos ~]#PING 0 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.044 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.062 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.049 ms
ld64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.041 ms

ld: no input files
[root@centos ~]#64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.050 ms
ls64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.043 ms

anaconda-ks.cfg  Documents  httpd-2.4.25          motd.txt  SC
b                Downloads  httpd-2.4.25.tar.bz2  Music     Templates
c                ff         initial-setup-ks.cfg  Pictures  Videos
Desktop          hello      jiaoben.sh            Public
[root@centos ~]#64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.052 ms
64 bytes from 127.0.0.1: icmp_seq=8 ttl=64 time=0.051 ms
fg 64 bytes from 127.0.0.1: icmp_seq=9 ttl=64 time=0.059 ms
1
ping 0
64 bytes from 127.0.0.1: icmp_seq=10 ttl=64 time=0.064 ms
l64 bytes from 127.0.0.1: icmp_seq=11 ttl=64 time=0.051 ms
s
64 bytes from 127.0.0.1: icmp_seq=12 ttl=64 time=0.050 ms
64 bytes from 127.0.0.1: icmp_seq=13 ttl=64 time=0.049 ms
64 bytes from 127.0.0.1: icmp_seq=14 ttl=64 time=0.061 ms
64 bytes from 127.0.0.1: icmp_seq=15 ttl=64 time=0.058 ms
^Z
[1]+  Stopped                 ping 0
[root@centos ~]#jobs
[1]+  Stopped                 ping 0
[root@centos ~]#fg 1
ping 0
64 bytes from 127.0.0.1: icmp_seq=16 ttl=64 time=0.035 ms
64 bytes from 127.0.0.1: icmp_seq=17 ttl=64 time=0.050 ms
64 bytes from 127.0.0.1: icmp_seq=18 ttl=64 time=0.047 ms
^Z
[1]+  Stopped                 ping 0
[root@centos ~]#pidof ping
4501
[root@centos ~]#kill -18 pid
-bash: kill: pid: arguments must be process or job IDs
[root@centos ~]#kill -18 4501
[root@centos ~]#64 bytes from 127.0.0.1: icmp_seq=19 ttl=64 time=0.726 ms
64 bytes from 127.0.0.1: icmp_seq=20 ttl=64 time=0.050 ms
64 bytes from 127.0.0.1: icmp_seq=21 ttl=64 time=0.058 ms
64 bytes from 127.0.0.1: icmp_seq=22 ttl=64 time=0.049 ms
64 bytes from 127.0.0.1: icmp_seq=23 ttl=64 time=0.048 ms
ls
anaconda-ks.cfg  Documents  httpd-2.4.25          motd.txt  SC
b                Downloads  httpd-2.4.25.tar.bz2  Music     Templates
c                ff         initial-setup-ks.cfg  Pictures  Videos
Desktop          hello      jiaoben.sh            Public
[root@centos ~]#64 bytes from 127.0.0.1: icmp_seq=24 ttl=64 time=0.050 ms
64 bytes from 127.0.0.1: icmp_seq=25 ttl=64 time=0.049 ms
fg 64 bytes from 127.0.0.1: icmp_seq=26 ttl=64 time=0.051 ms
1
ping 0
64 bytes from 127.0.0.1: icmp_seq=27 ttl=64 time=0.047 ms
64 bytes from 127.0.0.1: icmp_seq=28 ttl=64 time=0.050 ms
ls
64 bytes from 127.0.0.1: icmp_seq=29 ttl=64 time=0.052 ms
64 bytes from 127.0.0.1: icmp_seq=30 ttl=64 time=0.046 ms
64 bytes from 127.0.0.1: icmp_seq=31 ttl=64 time=0.048 ms
^C
--- 0 ping statistics ---
31 packets transmitted, 31 received, 0% packet loss, time 129711ms
rtt min/avg/max/mdev = 0.035/0.072/0.726/0.119 ms

同时运行多个进程,提高效率
方法1
vi all.sh
f1.sh&
f2.sh&
f3.sh&
方法2
(f1.sh&);(f2.sh&);(f3.sh&)
方法3
{ f1.sh& f2.sh& f3.sh& }

你可能感兴趣的:(kill)