Linux basic command - kill sends a signal to the process

Linux basic command -setfacl to set file ACL policy rules

Kill

1. Command introduction

First use the help documentation to view the command information

NAME
kill - terminate a process

killThe main function of the command is to send a signal to the process. Here we mainly use it to terminate the process, which has the same meaning as its English word. If you do not need to use a certain process in the Linux system, you can use this command to end the process immediately. You can use serviceor systemctlmanage commands stopto (stop) a service.

2. Command syntax

The basic syntax of this command is:kill 【参数】【进程号】

SYNOPSIS
kill [-s signal|-p] [-q sigval] [-a] [–] pid…
kill -l [signal]

3. Basic parameters

1. Common parameters of the command

display in representation

-l List the signals supported by the system
-s Specifies the signal to send to the process
-a The correspondence between commands and process numbers is not limited
-p don't send any signal

Meaning of common kill signals

Use kill -lto display all signals

[root@localhost ~]# 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	

From the picture above, we can see that there are really many signals that kill can support, but basically we don’t use that many signals. The following are more commonly used.

1) SIGHUP reload process
2)SIGINT Interrupt the process, similar to the ctrl+c shortcut key
3) NEXT exit process
9)SIGKILL Forcibly end the process, it is not recommended to use it if it is not necessary, because it may cause data loss or the terminal cannot be restored to the supported state
15) TERM End the process normally, which is the default command of the kill command

4. Reference examples

1. End the MySQL process

We use · top, psor netstatview MySQLthe process PIDto end the process normally.

[root@localhost ~]# netstat -antp | grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      4561/mysqld         
[root@localhost ~]# ps -ef | grep mysql
mysql      4561      1  0 14:43 ?        00:00:01 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
root      55203   2853  0 15:23 pts/1    00:00:00 grep --color=auto mysql
[root@localhost ~]# kill 15 4561
[root@localhost ~]# ps -ef | grep mysql
root      55242   2853  0 15:24 pts/1    00:00:00 grep --color=auto mysql
[root@localhost ~]# netstat -antp | grep mysqld

You can see that the service process has ended. If you are not sure whether the service has been closed, you can also use to systemctlview the service status.

[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 三 2023-03-08 15:25:43 CST; 5s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 55384 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 55361 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 55387 (code=exited, status=0/SUCCESS)

3月 08 15:25:26 localhost.localdomain systemd[1]: Starting MySQL Server...
3月 08 15:25:27 localhost.localdomain systemd[1]: Started MySQL Server.

2. Forcibly end the process

You can killadd -9parameters later to completely end unused processes.

kill -9 55790

5. The extended command of kill

killThe command also has other similar commands, such as: pkill, killall, let’s take a look at the use of these commands together. Here is a brief description of how to use these extended commands.

1. pkill

pkill command: process kill, the main function is to end the specified process according to the process name

> 用法:pkill 【参数】进程名
> 常用的命令参数
>  -P:	指定父进程号
>  -g:	指定进程组
>  -t:	指定开启进程的终端
>  -u:	结束指定用户的进程
> 例子:pkill prometheus   #直接pkill在后面加进程名删除
> 例子:pkill -u host   #根据拥有者终止进程

2. killall

killall command: Kill all, the main function is to end the process by name. Speaking of the previous kill command, it can only close the process and its child processes one by one, which is too cumbersome. This killall command only needs to give it one to close service name, it will automatically find all the corresponding process information, and close it directly. There are many functions that this command can achieve, let’s take a look

> 语法格式:killall 【参数】名称
> 常用的命令参数
>  -e:	 	对长名称进行精确匹配
>  -l :		打印所有已知的信号列表
>  -p:	 	结束进程所属的进程组
>  -i :	交互式结束进程,结束进程前需要进行确认
>  -r :	使用正则表达式匹配要结束的进程名称
>  -s :    用指定的进程号代替默认型号"SIGTERM"
>  -u :    结束指定用户的进程
>  
>  例子:[root@localhost ~]# killall -i vim  #交互式确认是否结束
		 杀死 vim(55790) ? (y/N) y
		 [root@localhost ~]# killall httpd  #指定服务结束所有进程
		 [root@localhost ~]# killall -l     #显示所有已知的信号列表
		 HUP INT QUIT ILL TRAP ABRT IOT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM
STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH IO PWR SYS
UNUSED

Summarize

The use of the kill command is actually to end a certain process. If you don’t want to use the process, you can use this command. If you think the content is okay, you can like it to support it!
insert image description here

Guess you like

Origin blog.csdn.net/rhn_111/article/details/129403522