The Kill Command in Linux is used to terminate the operation of a specified process. It is a common command for process management in Linux. Usually, the Ctrl+C key can be used to terminate a foreground process, but for a background process, we need to terminate with kill command. We need to use tools such as ps/pidof/pstree/top to get the process PID first, and then kill the process with kill command. The Kill Command terminates a process by sending a specified signal to it. By default, TERM signals numbered 15 are used. TERM signals terminate all processes that cannot capture the signal. For those processes that can capture the signal, kill the process by using a kill signal numbered 9.
1. Command format:
kill [parameter] [process number]
2. Command function:
Send the specified signal to the corresponding process. No specified model will send SIGTERM (15) to terminate the specified process. If the program can not be terminated at any time, the "-KILL" parameter can be used, and the signal it sends is SIGKILL(9), the process will be forced to terminate, and the process number can be viewed using ps command or jobs command. Root users will affect users'processes, while non-root users can only affect their own processes.
3. Command parameters:
- - L signal, if no signal numbering parameter is added, the "-l" parameter will list all the signal names.
- - a When dealing with the current process, there is no restriction on the correspondence between the command name and the process number
- - p Specifies that the kill Command prints only the process number of the process concerned, without sending any signals.
- - s Designated Send Signal
- - u Designated User
Be careful:
- The kill Command can be with or without a signal number option. 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. kill can also be used to send a specific signal to a process. For example:
kill -2 123
Its effect is the same as pressing Ctrl+C key when running the process with PID 123 in the foreground. However, ordinary users can only use kill commands without signal parameters or - 9 signals at most. - kill can take a process ID number as a parameter. When killing signals to these processes, they must be the masters. If you try to revoke a process that has no permission to revoke or a process that does not exist, you get an error message.
- Multiple processes can be signaled or terminated.
- When kill successfully sends a signal, the shell displays the termination information of the process on the screen. Sometimes this information will not be displayed immediately, only when the Enter key is pressed to make the shell's command prompt appear again.
- It should be noted that signals force processes to terminate, which often brings some side effects, such as data loss or terminal failure to return to normal state. Care must be taken when sending signals. Kill signals (9) are used only when absolutely necessary, because processes cannot capture them first. To undo all background jobs, type kill 0. Because some commands that run in the background will start multiple processes, it's troublesome to track and find the PID of all the processes to kill. At this point, kill 0 is an effective way to terminate all processes initiated by the current shell.
4. Use examples:
Example 1: List all signal names
Order:
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
Explain:
Only the ninth signal (SIGKILL) can terminate the process unconditionally, and all other signal processes have the right to ignore it. Here are the commonly used signals:
HUP 1 terminal disconnection
INT 2 interruption (same as Ctrl + C)
QUIT 3 Exit (same as Ctrl +)
Termination of TERM 15
KILL 9 Forced Termination
CONT 18 continues (as opposed to STOP, fg/bg command)
STOP 19 pauses (same as Ctrl + Z)
Example 2: Get the value of the specified signal
Order:
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]#
Explain:
Example 3: Find the process with ps and kill it
Order:
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) - No process
[root@localhost test6]#
Explain:
Example 4: Kill the process completely
Order:
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) - No process
[root@localhost test6]#
Explain:
Example 5: Kill all processes of a specified user
Order:
kill -9 $(ps -ef | grep peidalinux)
kill -u peidalinux
Output:
[root@localhost ~]# kill -9 $(ps -ef | grep peidalinux)
[root@localhost ~]# kill -u peidalinux
Explain:
Method 1, filter out the hnlinux user process and kill it
Example 6: The init process is not killable
Order:
kill -9 1
Output:
[root@localhost ~]# ps -ef|grep init
root 1 0 0 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 0 0 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 0 0 Nov02 ? 00:00:00 init [3]
root 17567 17534 0 17:38 pts/1 00:00:00 grep init
[root@localhost ~]#
Explain:
Init is one of the indispensable programs in Linux system operation. The so-called init process is a user-level process initiated by the kernel. After the kernel starts itself (has been loaded into memory, started running, and initialized all device drivers and data structures, etc.), it completes the boot process by starting a user-level program init. So 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 not killable!