How to KILL a process from the command line in Linux?
The Linux command line has all the tools you need to stop a running process. Jack Wallen tells you the details.
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.
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)
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
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.
Figure 2: Use the ps command to locate the required content information.
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.
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
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
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
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.
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!

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.

Introduction Linux is a powerful operating system favored by developers, system administrators, and power users due to its flexibility and efficiency. However, frequently using long and complex commands can be tedious and er

Linux is suitable for servers, development environments, and embedded systems. 1. As a server operating system, Linux is stable and efficient, and is often used to deploy high-concurrency applications. 2. As a development environment, Linux provides efficient command line tools and package management systems to improve development efficiency. 3. In embedded systems, Linux is lightweight and customizable, suitable for environments with limited resources.

Introduction: Securing the Digital Frontier with Linux-Based Ethical Hacking In our increasingly interconnected world, cybersecurity is paramount. Ethical hacking and penetration testing are vital for proactively identifying and mitigating vulnerabi

The methods for basic Linux learning from scratch include: 1. Understand the file system and command line interface, 2. Master basic commands such as ls, cd, mkdir, 3. Learn file operations, such as creating and editing files, 4. Explore advanced usage such as pipelines and grep commands, 5. Master debugging skills and performance optimization, 6. Continuously improve skills through practice and exploration.

Linux is widely used in servers, embedded systems and desktop environments. 1) In the server field, Linux has become an ideal choice for hosting websites, databases and applications due to its stability and security. 2) In embedded systems, Linux is popular for its high customization and efficiency. 3) In the desktop environment, Linux provides a variety of desktop environments to meet the needs of different users.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!