Unlike Windows, where a frozen application can usually be closed by frantically clicking the red X or pressing Alt-F4, a crashed background program on a headless Linux server will simply run infinitely until you explicitly order the operating system to execute it.
When a web server hangs or a database locks up, you must use the command line to forcefully terminate the software. Linux provides two primary tools for this job: the surgical kill command, and the blunt-force killall command.
Step 1: Find the Process ID (PID)
Before you can tell Linux to kill a specific program, you have to tell it exactly which program to target. The operating system does not identify running software by its human-readable name; it assigns every single running program a unique numerical ID, known as a Process ID (PID).
To find the PID of the frozen software, you must use the ps (Process Status) command combined with grep.
For example, if you know the Apache web server has crashed, you would type:
ps aux | grep apache
The terminal will print a list of every running process containing the word “apache.” Look at the second column from the left. You will see a number (e.g., 1458). This is the PID.
Method 1: The Surgical kill Command
Now that you know the exact numerical ID of the frozen program, you can use the standard kill command to terminate it.
sudo kill 1458
By default, the kill command is relatively polite. It sends a “SIGTERM” (Signal Terminate) message to the program, which basically says: “Please save your work, close your files, and shut yourself down safely.”
The Nuclear Option: kill -9
If a program is severely crashed, it will completely ignore the polite SIGTERM request and continue running. When this happens, you must use the nuclear option: the -9 flag. This sends a “SIGKILL” signal. It bypasses the program entirely, goes straight to the Linux kernel, and commands the operating system to instantly execute the process without giving it a chance to save anything.
sudo kill -9 1458
Use this sparingly, as it can cause data corruption in databases if they are suddenly executed mid-write.
Method 2: The Blunt-Force killall Command
If a massive piece of software (like Google Chrome or a complex database) crashes, you will quickly discover a problem when you run the ps aux command: the software hasn’t spawned just one PID. It has spawned twenty different sub-processes, all with different PIDs.
Typing kill 1458, then kill 1459, then kill 1460 is incredibly tedious. Instead, you can use the killall command. This command completely ignores numerical PIDs and allows you to target the software by its human-readable name, instantly executing every single process associated with that name simultaneously.
sudo killall apache2
Just like the standard kill command, you can also force a SIGKILL by adding the -9 flag if the processes are stubbornly refusing to close.
sudo killall -9 apache2