1. Role
The kill command is used to stop a process.
2. Format
Kill [-S Signal |-p] [-A] PID...
Kill-L [Signal]
3. Parameters
-S: Specifies the sent signal.
-P: analog transmission signal.
-L: Name List of the specified signal.
PID: ID of the process to be aborted.
Signal: indicates the signal.
4. Description
Process is a very important concept in Linux. Linux is a multi-task operating system that often runs multiple processes at the same time. We don't care how these processes are allocated, orKernelHow to manage and allocate time slices is concerned with how to control these processes so that they can serve users well.
The Linux operating system includes three different types of processes, each of which has its own characteristics and attributes. An interactive process is composedShellStart the process. Interactive processes can run either on the foreground or on the background. The batch processing process is a process sequence that is not associated with the terminal. Processes started when the Linux system is started when the monitoring process (also known as the system daemon process) is running in the background. For example, httpd is a monitoring process of the famous Apache server.
The kill command works by sending a system operation signal and the process ID of a program to the Linux kernel. Then, the system kernel can operate the process specified by the process ID number. For exampleTopCommand, we can see that the system runs many processes, sometimes you need to use kill to stop some processes to improve system resources. When explaining the installation and login commands, I mentioned that the role of multiple virtual consoles in the system is to switch to other virtual consoles to close the program when a program error causes a system deadlock. In this case, the command is kill, because kill can be directly called by most internal shell commands.
5. Application Instance
(1) force stop (often used to kill) a process with the process ID 324:
# Kill-9 324
(2) Remove the deadlock in Linux
In Linux, a program crashes and is in a deadlock state. In this case, you do not need to restart the computer. You only need to stop (or close) the problematic program. When kill is on the x-window interface, the main program (except the crashed program) is generally started normally. Open a terminal and stop the problematic program there. For example, if the Mozilla browser program is locked, you can use the kill command to stop all programs that contain the mozolla browser. First, use the top command to investigate the PID of the program, and then use the kill command to stop the program:
# Kill-sigkill xxx
Xxx indicates the process ID of the program with the mozolla browser.
(3) Use commands to recycle memory
We know that memory is very important to the system. memory recovery can improve system resources. The kill command can be used to abort programs that are out of track or do not have the corresponding programs for a long time. For example, if you use the top command to find a useless (zombie) process, you can use the following command:
# Kill-9 xxx
XXX is a useless process ID.
Run the following command:
# Free
The available memory capacity is increased.
(4) killall command
In Linux, A killall command is also provided to directly use the process name instead of the process ID number. For example:
# Killall-hup inetd
* The safest way to kill a processThe kill command is used without a modifier or a flag.
First, use the PS-Ef command to determine the PID of the process to be killed, and then enter the following command:
# Kill-PID
Note: Standard kill commands can achieve the goal. Terminate the problematic process and release the process resources to the system. However, if a child process is started and only the parent process is killed, the child process is still running and consumes resources. To prevent these so-called "zombie processes", ensure that all child processes are killed before the parent process is killed.
* Determine the PID or ppid of the process to be killed
# Ps-Ef | grep httpd
* End the process in an elegant way
# Kill-l PID
The-L option tells the kill command to end the process as if the user who started the process had logged out. When this option is used, the kill command also tries to kill the left child process. But this command is not always successful-you may still need to manually kill the child process and then kill the parent process.
* Term Signal
Send a term signal to the parent process, trying to kill it and its child process.
# Kill-term ppid
* Killall command
Killall command to kill all processes in the same process group. It allows you to specify the name of the process to be terminated, rather than the PID.
# Killall httpd
*Stop and restart Processes
Sometimes you only want to simply stop and restart the process. As follows:
# Kill-HUP PID
This command causes Linux to shut down the slow execution process and then restart immediately. This command is convenient when you configure the application. You can execute this command when you need to restart the process after modifying the configuration file.
* Kill-9 PID
Agreed kill-s sigkill
This powerful and dangerous Command forces the process to terminate suddenly at run time, and the process cannot be cleaned up after it ends. The hazard is that the system resources cannot be released normally. It is generally not recommended unless other methods are ineffective.
When using this command, you must use PS-Ef to confirm that no zombie process is left. Only the parent process can be terminated to eliminate zombie processes. If the zombie process is adopted by init, the problem will be serious. Killing the INIT process means shutting down the system.
If there is a zombie process in the system, and its parent process is INIT, And the zombie process occupies a large amount of system resources, you need to restart the machine at some time to clear the process.