Unlike standard desktop operating systems where you can just click the red “X” to close a frozen application, Linux servers operate primarily through the command line. If a web server crashes, a Python script gets stuck in an infinite loop, or a database query consumes 100% of your CPU, you cannot use a mouse to stop it. You must ruthlessly terminate the rogue process using the terminal.
The Linux kernel provides three distinct commands for terminating applications: kill, pkill, and killall. Here is how to use them safely without crashing the entire server.
Method 1: Find the PID and use “kill” (The Safest Way)
The standard kill command is a precision sniper rifle. It targets one exact, specific instance of a program. To use it, you must first discover the unique Process ID (PID) number that the kernel assigned to the rogue application.
Step 1: Find the PID
There are several ways to list running processes, but the fastest is to combine the ps command with a grep search.
- Open your terminal and type this command (replacing “nginx” with the name of the program that is stuck):
ps aux | grep nginx
- Press Enter.
The terminal will print a list of running processes that match the word “nginx.” Look closely at the resulting table. The very second column from the left is the PID (a number like 12345).
Step 2: Send the Kill Signal
Now that you know the exact PID, you can terminate it.
- Type the kill command followed by the PID:
sudo kill 12345
- Press Enter.
By default, this sends a polite SIGTERM (15) signal to the program, essentially asking it to save its data and close itself down cleanly. If the program is completely frozen, it will ignore this polite request.
Step 3: The Nuclear Option (SIGKILL)
If the program refuses to die, you must bypass the polite request and instruct the Linux kernel to immediately sever its memory access.
sudo kill -9 12345
The -9 flag sends the SIGKILL signal. The program has zero ability to block this; it will instantly vanish from existence. Warning: Any unsaved data within that program will be permanently destroyed.
Method 2: Target by Name Using “pkill” (The Faster Way)
If you do not want to bother searching for a specific PID number, you can use the pkill command to shoot down a program using just its name.
sudo pkill nginx
Warning: pkill is a shotgun, not a sniper rifle. It will search the entire process table and instantly terminate every single process that has the word “nginx” in its name. If you have 50 different Nginx worker threads running, this single command will obliterate all of them simultaneously. Only use this if you want to wipe out the entire application.
You can also attach the nuclear -9 flag to this command:
sudo pkill -9 nginx
Method 3: “killall” (Exact Name Matching)
The killall command is very similar to pkill, but it is much stricter. While pkill will kill any process that contains your search term (e.g., pkill java might accidentally kill a script named javascript-compiler), killall requires an exact, perfect match.
sudo killall firefox
This will only terminate processes specifically named “firefox.” It is much safer to use on a production server when you are trying to wipe out a specific application group but want to avoid friendly fire.
Method 4: The Visual Way (Using “top”)
If you don’t know the name of the program, but your server fans are screaming because the CPU is pinned at 100%, you can use a live dashboard to find and kill the culprit.
- Type
topand press Enter. A live, self-updating dashboard will take over the screen. - Look at the very top of the list. The program consuming the most CPU power will be aggressively flashing at the #1 spot.
- Note the PID number of the runaway program.
- Without exiting the dashboard, press the
kkey on your keyboard. - A prompt will appear asking “PID to signal/kill.” Type the PID number and press Enter.
- A secondary prompt will ask what signal to send (it defaults to 15). Press Enter to politely kill it, or type 9 and press Enter to instantly nuke it.
- Press
qto exit the dashboard.