Home > System Tutorial > LINUX > How to KILL a process from the command line in Linux?

How to KILL a process from the command line in Linux?

王林
Release: 2024-03-23 14:50:03
forward
1119 people have browsed it

The Linux command line has all the tools you need to stop a running process. Jack Wallen tells you the details.
How to KILL a process from the command line in Linux?

Imagine this: you open a program (perhaps from your desktop menu or command line), and then start using the program, but you don't expect that the program will lock up, stop running, or crash unexpectedly. You try to run the program again, but it reports back that the original process did not shut down completely.

what should you do? You want to end the process. But how? Believe it or not, the best solutions are mostly in the command line. Thankfully, Linux has every necessary tool for users to kill the errant process, however, before you can execute the command to kill the process, you first need to know what the process is. How to handle this type of task. Once you get the hang of this tool, it's actually pretty simple...

The steps I'm going to outline are applicable to every Linux distribution, whether desktop or server. I'm going to limit myself to the command line, so please open your terminal and start typing commands.

Location process

The first step to kill an unresponsive process is to locate the process. There are two commands I use to locate processes: top and ps commands. top is a tool that every system administrator knows. With the top command, you can know all the currently running processes. In the command line, enter the top command to see the program process you are running (Figure 1)

How to KILL a process from the command line in Linux?

Figure 1: The top command gives you a lot of information.

You can see very important information from the displayed list. For example, the Chrome browser is slow to respond. According to our top command, we can identify four Chrome browser processes running. The pid numbers are 3827, 3919, 10764 and 11679 respectively. This information is important and can be used in a special way to end the process.

Although the top command is very convenient, it is not the most efficient way to get the information you want. You know which Chrome process you want to kill, and you don't want to see the real-time information displayed by the top command. For this reason, you can use the ps command and then the grep command to filter out the output. The ps command displays a snapshot of the current process list, and then uses the grep command to output matching patterns. The reason why we filter the output of the ps command through the grep command is simple: if you just enter the ps command, you will get a snapshot of the list of all current processes, and what we need is to list the Chrome browser process related. So the command looks like this:

ps aux | grep chrome
Copy after login

Here the aux options are as follows:

  • a = Show processes for all users
  • u = Display the user and owner of the process
  • x = Also show processes that are not attached to the terminal

This x parameter is important when you search for information about graphical programs.

When you enter the above command, you will get more information than Figure 2, and it is sometimes more effective than the top command.

How to KILL a process from the command line in Linux?

Figure 2: Use the ps command to locate the required content information.

end process

Now we start the task of ending the process. We have two pieces of information that can help us kill the errant process.

  • Name of the process
  • Process ID (PID)

Which one you use will determine how the terminal command is used. There are usually two commands to end the process:

  • kill - Kill a process by process ID
  • killall - Kill a process by its name

There are two different signals that can be sent to these two commands to end the process. The signal you send determines the results you want from the end process command. For example, you can send a HUP signal to a command that ends a process, and the command will actually restart the process. This is a wise choice when you need to restart a process immediately (for example, in the case of a daemon). You can get a list of all signals by typing kill -l and you will find a large number of signals.

How to KILL a process from the command line in Linux?

Figure 3: Available end-process signals.

The most commonly used signal to end a process is:

The nice thing is that you can use the signal value instead of the signal name. So you don't have to remember all the various signal names.

So, let us now use the kill command to kill the Chrome browser process. The structure of this command is:

kill SIGNAL PID
Copy after login

Here SIGNAL is the signal to be sent, and PID is the ID of the process to be killed. We already know that the process ID numbers from our ps command that we want to kill are 3827, 3919, 10764, and 11679. So to send the end process signal, we enter the following command:

kill -9 3827
kill -9 3919
kill -9 10764
kill -9 11679
Copy after login

Once we enter the above command, all processes of the Chrome browser will be successfully killed.

We have an easier way! If we already know the name of the process we want to kill, we can send the same signal using the killall command, like this:

killall -9 chrome
Copy after login

As a side note, the above command may not capture all running Chrome processes. If, after running the above command, you enter the ps aux | grep chrome command to filter and see what are the remaining running Chrome processes, the best way is to return to the kIll command to send the signal value 9 through the process ID. End this process.

Ending a process is easy

As you can see, killing the wrong process is not as challenging as you originally thought. When I'm letting a stubborn process die, I tend to use the killall command as the efficient way to kill it, however, when I'm letting a truly active process die, the kill command is a good way to go.


The above is the detailed content of How to KILL a process from the command line in Linux?. For more information, please follow other related articles on the PHP Chinese website!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template