1. kill, killall, pkill, and xkill tools for terminating Processes
Terminate a process or terminate a running program, usually through kill, killall, pkill, xkill, etc. For example, if a program is dead but cannot exit, you should consider using these tools.
In addition, in server management, you can use these tools to stop the parent process that does not involve database server programs. Why cannot the parent process of the database server be killed using these tools? The reason is simple. When these tools force terminate the database server, more file fragments will be generated for the database. When the fragmentation reaches a certain level, the database may crash. For example, it is best to close the mysql server according to its normal program, instead of using pkill mysqld or killall mysqld as dangerous actions. Of course, we should use kill to kill database sub-processes that occupy too much resources.
1. kill
Kill applications are used together with ps or pgrep commands;
Kill usage:
Kill [signal code] process ID
Note: The signal code can be omitted. the commonly used signal code is-9, indicating forced termination;
Example:
[Root @ localhost ~] # Ps auxf | grep httpd
Root 4939 0.0 0.0 5160 708 pts/3 S + \ _ grep httpd
Root 4830 0.1 1.3 24232 10272? Ss/usr/sbin/httpd
Apache 4833 0.0 0.6 24364 4932? S \ _/usr/sbin/httpd
Apache 4834 0.0 0.6 24364 4928? S \ _/usr/sbin/httpd
Apache 4835 0.0 0.6 24364 4928? S \ _/usr/sbin/httpd
Apache 4836 0.0 0.6 24364 4928? S \ _/usr/sbin/httpd
Apache 4840 0.0 0.6 24364 4928? S \ _/usr/sbin/httpd
Check the httpd server process. You can also use pgrep-l httpd to view the process;
Let's take a look at the second column in the above example, that is, the PID column of the process, where 4830 is the parent process of the httpd server, and all processes from 4833-4840 are their 4830 sub-processes; if we kill the parent process 4830, the child process under it will also die;
[Root @ localhost ~] # Kill 4840 Note: kill the 4840 process;
[Root @ localhost ~] # Ps-auxf | grep httpd Note: What are the results? Is the httpd server still running?
[Root @ localhost ~] # Kill 4830 Note: kill the httpd parent process;
[Root @ localhost ~] # Ps-aux | grep httpd Note: Check whether other httpd sub-processes exist and whether the httpd server is still running?
For zombie processes, kill-9 can be used to force termination and exit;
For example, if a program has completely died and kill fails to exit without signal strength, the best way is to add signal strength-9, followed by killing the parent process; for example;
[Root @ localhost ~] # Ps aux | grep gaim
Beinan 5031 9.0 2.3 104996 17484? S gaim
Root 5036 0.0 0.0 5160 724 pts/3 S + grep gaim
Or [root @ localhost ~] # Pgrep-l gaim
5031 gaim
[Root @ localhost ~] # Kill-9 5031
2. killall
Killall uses the program name to directly kill all processes. Let's just talk about it.
Usage: name of the program that is running killall
Killall can also be used with ps or pgrep for convenience. You can use ps or pgrep to check which programs are running;
Example:
[Root @ localhost beinan] # pgrep-l gaim
2979 gaim
[Root @ localhost beinan] # killall gaim
3. pkill
The pkill method is similar to the killall method. It also directly kills the running program. If you want to kill a single process, use kill to kill it.
Application Method:
# Pkill name of the running program
Example:
[Root @ localhost beinan] # pgrep-l gaim
2979 gaim
[Root @ localhost beinan] # pkill gaim
4. xkill
Xkill is a program used on the desktop to kill the graphic interface. For example, when firefox crashes and cannot exit, you can click the mouse to kill firefox. When xkill is running and the icon of the personal brain is displayed, it will be okay if any graphics program crashes. If you want to terminate xkill, right-click to cancel it;
Xkill call method:
[Root @ localhost ~] # Xkill
Ii. Linux kill command and Signal Control
Signals are used to communicate with the daemon and process. Any active task is a process, and the daemon is a background service waiting to respond to certain events or execute tasks according to the schedule. A program must have a signal processing program built in it for capturing and responding to signals. The signal Reference Guide in LINUX explains various signals and their usage. The signal is sent by the "kill" command. The kill-l command displays a list of available signals and their numbers.
All daemon and processes have a process ID (PID), for example, the content displayed after the ps name is used:
$ Ps aux
User pid % CPU % MEM TTY STAT COMMAND
Root 1 0.0 0.1? S init [2]
105 7783 0.0 0.2? Ss/usr/bin/container-daemon -- system
Hal 7796 0.0 0.7? Ss/usr/sbin/hald
Postfix 7957 0.0 0.2? S qmgr-l-t fifo-u-c
Nagios 8371 0.0 0.2? SNs/usr/sbin/nagios/etc/nagios. cfg
This output is simplified. You can see more rows and columns in the system. If some processes consume all your CPU or memory, you can find them in the output % CPU and % MEM columns. A quicker way to find out a process that is out of control is to use the top command, because the process that uses the most CPU resources is displayed at the top according to the default settings. We can use the "yes" command to test it:
$ Yes carla is teh awesum
This command will repeatedly display "carla is teh awesum" at a high speed until you stop running it. This will enable your CPU usage to reach the threshold.
$ Top
...
Pid user pr ni virt res shr s % CPU % mem time + COMMAND
12144 carla 25 0 31592 17 m 13 m R 93.4 0: 50. 26 konsole
22236 carla 15 0 2860 468 S 400 4.3. 97 yes
After analyzing this result, you will find some interesting things. You will find that the program that consumes the most CPU is the konsole virtual terminal program, rather than the "yes" command, this is because the "yes" command is run in the konsole terminal program. If you run the same command sequence in a "real" Console (Press Ctrl + alt + f2), you will see that the "yes" command is ranked first.
There are many ways to stop the "yes" command. If you want to return to the shell that runs it, press CTRL + c. Alternatively, you can use the "kill" command in another shell to stop running the "yes" command. The "Kill" command is followed by the PID or command name, as shown below:
$ Killed 22236
Or
$ Killall yes
Press CTRL + c to send a SIGINT (Signal 2), which is an interrupt signal that the keyboard requires control. The kill and killall commands send a SIGTERM signal (number 15) according to the default settings ). In the program, the SIGTERM signal (15) can be captured or ignored, or interpreted in different ways. Therefore, if your program's response to the KILL command is different from your expectation, it may be a problem with the target program to be killed.
Terminating a parent process usually terminates its child process. However, this is not always the case. Do you know what a sub-process is? Add the-f option to the ps command, as shown below:
$ Ps axf
22371? R :35 _ konsole [kdeinit]
22372 pts/3 Ss | _/bin/bash
24322 pts/3 S + | _ yes carla is teh awesum
22381 pts/4 Rs | _/bin/bash
24323 pts/4 R + | _ ps axf
Now, go back to the SIGHUP topic
SIGHUP is pronounced as "sig-hup", abbreviated as signal hangup, meaning "stop signal ". How do you send a SIGHUP signal? There are several methods:
# Kill-HUP [pid]
# Killall-HUP [process-name]
# Kill-1 [pid]
# Killall-1 [process-name]
Therefore, you can use the PID or name, signal name or number. So why not use the/etc/init. d/foo command to restart the system? Using their own init (initialization) files to control the service is a preferred method, because these files usually contain sound and error checks and additional features. The main reason for using the "kill" command and signal is to terminate the suspended and out-of-control processes as clearly as possible without restarting or logging out.
Terminate a process
As you can see in the man page of the signal, there are more than a dozen ways to control the process. Below are some common methods:
Kill-STOP [pid]
Send SIGSTOP (17,19, 23) to stop a process without killing it.
Kill-CONT [pid]
Send SIGCONT (, 25) to start a stopped process again.
Kill-KILL [pid]
Send SIGKILL (9) to force the process to stop immediately without cleaning.
Kill-9-1
Terminate all processes you have.
The SIGKILL and SIGSTOP signals cannot be captured, blocked, or ignored. However, other signals are acceptable. So this is your weapon.
Bash shell Kil command l
The Bash shell contains a built-in kill command. When you execute the following command:
$ Type-all kill
Kill is a shell built-in
Kill is/bin/kill
The command results show that there are two kill commands, one is the built-in BASH command, and the other is the/bin/kill executable program. Generally, these two commands may not conflict with each other. However, if you encounter a kill command exception, you can specify the/bin/kill command explicitly.
You must refer to the reference resources listed in the following resources to learn more about kill in Linux, because this is your ticket to enter the field of Linux system maintenance. This knowledge allows you to maintain the system like a surgical procedure, instead of restarting the system every time you encounter a problem, as we know about some of the poor operating systems.
Resources
Chapter 7 "start and end Linux" in Linux Cookbook"
Bash (1)-GNU Bourne-Again Shell
Yes (1)-print characters repeatedly before termination
Signal (7)-available signal list
Ps (1)-report the snapshot of the current process
Kill (1)-sends a signal to a process
Killall (1)-Kill processes by name
Pkill (1)-view or send a process Signal Based on the name and other attributes
Skill (1)-send a signal or report the Process status
Xkill (1)-destroy a customer program based on X Resources
Blog by Li Changchun