Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the kill command in Linux

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use the kill command in Linux. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

The Linux kill command is used to delete a program or work in progress. Kill can send the specified information to the program. The default information is SIGTERM (15), which terminates the specified program.

1. Command format:

Kill parameter

2. Command function:

Sends the specified signal to the appropriate process. If you do not specify a model, SIGTERM (15) will be sent to terminate the specified process. If the program cannot be terminated, the "- KILL" parameter, which sends a signal of SIGKILL (9), will force the process to end, and the process number can be checked using the ps command or the jobs command. Root users will affect their processes, while non-root users can only affect their own processes.

3. Command parameters:

-l signal, if you do not add the numbering parameter of the signal, using the "- l" parameter will list all the signal names

-a when dealing with the current process, the correspondence between the command name and the process number is not restricted

-p specifies that the kill command only prints the process number of the relevant process and does not send any signal

-s specifies the transmission signal

-u specified user

Note:

1. The kill command can have the option of signal number or not. If there is no signal number, the kill command sends a termination signal (15), which can be captured by the process so that the process can clean up and release resources before exiting. You can also use kill to send specific signals to the process. For example:

Kill-2 123

Its effect is equivalent to pressing the Ctrl+C key when the process with PID 123 is running in the foreground. However, the average user can only use the kill command without the signal parameter or the-9 signal at most.

2. Kill can take the process ID number as a parameter. When you use kill to signal these processes, you must be the owner of these processes. If you try to undo a process that does not have permission to undo or undo a process that does not exist, you will get an error message.

3. You can signal or terminate multiple processes.

4. When kill successfully sends the signal, shell will display the termination information of the process on the screen. Sometimes this message is not displayed immediately, but only when you press the Enter key to make the shell command prompt appear again.

5. It should be noted that the signal forcibly terminates the process, which often brings some side effects, such as data loss or terminal failure to return to normal state. You must be careful when sending a signal, using the kill signal (9) only as a last resort, because the process cannot capture it first. To undo all background jobs, type kill 0. Because some commands running in the background start multiple processes, it is troublesome to track and find the PID of all the processes to be killed. At this point, it is effective to use kill 0 to terminate all processes started by the current shell.

4. Use an example:

Example 1: list all signal names

Command:

Kill-l

Output:

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

Description:

Only the ninth signal (SIGKILL) can terminate the process unconditionally, and other signal processes have the right to ignore it. Here are some common signals:

HUP 1 terminal disconnected

INT 2 interrupt (same as Ctrl + C)

QUIT 3 exit (same as Ctrl +\)

TERM 15 termination

KILL 9 forced termination

CONT 18 continues (as opposed to STOP, fg/bg command)

STOP 19 pause (same as Ctrl + Z)

Example 2: get the value of the specified signal

Command:

Output:

[root@localhost test6] # kill-l KILL

9 [root@localhost test6] # kill-l SIGKILL

9 [root@localhost test6] # kill-l TERM

15 [root@localhost test6] # kill-l SIGTERM

15 [root@localhost test6] #

Description:

Example 3: first use ps to find the process, and then kill it with kill

Command:

Kill 3268

Output:

[root@localhost test6] # ps-ef | grep vim

Root 3268 2884 0 16:21 pts/1 00:00:00 vim install.log

Root 3370 2822 0 16:21 pts/0 00:00:00 grep vim

[root@localhost test6] # kill 3268

[root@localhost test6] # kill 3268

-bash: kill: (3268)-there is no such process

[root@localhost test6] #

Description:

Example 4: kill the process completely

Command:

Kill-9 3268

Output:

[root@localhost test6] # ps-ef | grep vim

Root 3268 2884 0 16:21 pts/1 00:00:00 vim install.log

Root 3370 2822 0 16:21 pts/0 00:00:00 grep vim

[root@localhost test6] # kill-9 3268

[root@localhost test6] # kill 3268

-bash: kill: (3268)-there is no such process

[root@localhost test6] #

Description:

Example 5: kill all processes of the specified user

Command:

Kill-9 $(ps-ef | grep peidalinux)

Kill-u peidalinux

Output:

[root@localhost ~] # kill-9 $(ps-ef | grep peidalinux)

[root@localhost] # kill-u peidalinux

Description:

Method one, filter out the hnlinux user process and kill

The instance 6:init process is unkillable

Command:

Kill-9 1

Output:

[root@localhost ~] # ps-ef | grep init

Root 1 00 Nov02? 00:00:00 init [3]

Root 17563 17534 0 17:37 pts/1 00:00:00 grep init

[root@localhost] # kill-9 1

[root@localhost ~] # kill-HUP 1

[root@localhost ~] # ps-ef | grep init

Root 1 00 Nov02? 00:00:00 init [3]

Root 17565 17534 0 17:38 pts/1 00:00:00 grep init

[root@localhost ~] # kill-KILL 1

[root@localhost ~] # ps-ef | grep init

Root 1 00 Nov02? 00:00:00 init [3]

Root 17567 17534 0 17:38 pts/1 00:00:00 grep init

[root@localhost ~] #

Description:

Init is one of the indispensable programs in the operation of Linux system. The so-called init process is a user-level process started by the kernel. After the kernel starts itself (has been loaded into memory, starts running, and has initialized all device drivers and data structures, etc.), it completes the boot process by starting a user-level program, init. Therefore, init is always the first process (its process number is always 1). All other processes are descendants of the init process. The init process is unkillable!

This is the end of the article on "how to use kill commands in Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report