Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
In Linux, some applications may block or run abnormally. In this case, you can run the kill command to end related processes. Let’s take a look at the various ways the KILL application process works.
kill
Perform akill
Command is actually sending a signal to the system to terminate some abnormal application process. You can usekill -l
Command to see a list of all signals:Here are the meanings of some common signals:
(1)SIGHUP terminal line hanging up, Stop process (2)SIGINT Stops process (same as Ctrl + C) (3)SIGQUIT Exits process (same as Ctrl + \) (15)SIGTERM Terminates process (9)SIGKILL Forcibly terminates process (18)SIGCONT Continue (as opposed to SIGSTOP, fg/bg command) (19)SIGSTOP pause (same as Ctrl + Z)Copy the code
Here we only need to know (15)SIGTERM and (9)SIGKILL.
(15)SIGTERM: Execute kill -15 PID command, where “-15” represents SIGTERM signal. Note: SIGTERM is the default option, meaning that executing kill PID is equivalent to executing kill -15 PID. Execute the instructions in the operating system will send a SIGTERM signal to the corresponding program, after program receives the signal, can be normally closed with a period of time, usually save progress and release resources, and then stopped, which is not necessarily immediately stop processes, such as the program is waiting for IO, may not stop running immediately. In other words, (15)SIGTERM signal is not a forced stop and can be ignored.
(9)SIGKILL: Execute the kill -9 PID command, where “-9” represents the SIGKILL signal. Is a kill signal that forces the process to stop immediately. The program cannot ignore this signal, and unsaved progress will be lost, which may affect the restart of the service. Use this command with caution.
The syntax for kill is as follows:
Kill signal or option PIDCopy the code
The default signal (when not specified) is SIGTERM. When it does not work, you can use the kill -9 pid command to force a process to be killed.
The application PID can be viewed using the following command:
ps -ef
Copy the code
or
ps -aux
Copy the code
For example, if you want to kill a process in Chrome, just execute itkill -9 21066
Can.
The problem is that if there are fewer processes, it doesn’t matter. Once there are more processes, it is very difficult to find the process to kill in a heap of process information.
You can pipe the ps query result to grep to find a process that contains a particular string. Pipe | used to separate two commands, pipe the output of the command on the left will act as a pipe, the right of the command input.
ps -aux | grep chrome
Copy the code
If you can’t remember the preceding command, you can use pgrep. The p of pgrep indicates that this command is grep specifically for process query.
pgrep chrome
Copy the code
If you want to display both the process name and pid, you can add the -l parameter:pgrep -l chrome
.
pkill
The pkill command allows extended regular expressions and other matching methods that can be killed using the application’s process name rather than pid. For example, to kill Chrome, simply run the following command:
pkill chrome
Copy the code
You can also enter part of the process name, for example:
pkill chro
Copy the code
Pkill looks like a combination of pgrep and kill.
Note: When using pkill, to avoid killing the wrong process, you should first use the pgrep -l process name to see the list of matches.
killall
Killall is similar to pkill, except that killall reports an error if an incomplete process name is given, whereas pkill only gives part of the process name. For example, usingkillall chro
Chro: process not found chro: process not foundkillall chrome
Can.
xkill
Xkill is a desktop application that kills a graphical interface. When you type “xkill” on the terminal, your cursor changes. Just click on the application and it will kill the application immediately.
conclusion
By using these “kill” commands, we can better handle applications that don’t work properly, thus avoiding system crashes and outages.
Original is not easy, if small partners feel helpful, please click a “like” and then go ~
Finally, thank my girlfriend for her tolerance, understanding and support in work and life!