When an application freezes, a background script enters an infinite loop, or a database query consumes 100% of your CPU, it can drag the entire Ubuntu operating system to a halt. In a graphical desktop environment, you might rely on the System Monitor app, but if you are managing a headless server via SSH, you must resolve the crisis through the command line.
Linux provides powerful tools to identify the exact process causing the issue and forcefully terminate (kill) it, immediately freeing up system resources without requiring a full reboot.
Step 1: Identify the Process ID (PID)
Before you can terminate a program, you must find its unique Process ID (PID). The operating system uses this number to track every running application.
Method A: Using ‘top’
If the system is running slowly and you do not know which application is at fault, the top command provides a live, real-time leaderboard of the most resource-intensive tasks.
- Type
topin your terminal and press Enter. - Look at the far left column labelled PID, and the far right column labelled COMMAND.
- Identify the misbehaving program and note its PID number.
- Press the Q key to exit the live monitor.
Method B: Using ‘pgrep’ or ‘pidof’
If you already know the exact name of the application that has frozen (for example, Nginx or Firefox), you can query its PID instantly without searching through lists.
- Type
pgrep firefox(replace ‘firefox’ with your application’s name). - The terminal will output the exact PID number on the next line.
Step 2: Kill the Process
Once you have the PID (for example, 4021), you can use the kill command to send a termination signal to the application.
The Standard Kill (SIGTERM)
The safest way to stop an application is to send a “Termination” signal. This politely asks the program to save its data, close its open files, and shut down gracefully.
kill 4021
Wait a few seconds and run pgrep again to see if the PID has disappeared.
The Force Kill (SIGKILL)
If the application is completely locked up and ignores the polite termination request, you must use the “Force Kill” signal (Signal 9). This tells the Linux kernel to immediately destroy the process without giving the application any time to react. Warning: This can result in unsaved data loss for that specific application.
kill -9 4021
Alternative: Using ‘killall’
If an application has spawned dozens of worker processes (like Google Chrome or Apache), hunting down and typing every single PID is highly inefficient. The killall command allows you to terminate every process associated with a specific name simultaneously.
killall firefox
Just like the standard command, you can force kill all instances by adding the -9 flag:
killall -9 firefox
Note: If the frozen process is owned by the root user (such as a core system daemon), you must prefix all of these commands with sudo (e.g., sudo kill -9 4021).