How to Kill a Process in Linux: Simple Commands Explained
To kill a process in Linux, use the
kill command followed by the process ID (PID), like kill 1234. You can also use killall with the process name, for example killall firefox, to stop all processes with that name.Syntax
The kill command stops a process by sending it a signal. The basic syntax is:
kill [signal] PID- sends a signal to the process with the given PID.killall [signal] process_name- sends a signal to all processes with the given name.
Common signals include SIGTERM (default, politely asks to stop) and SIGKILL (forces immediate stop).
bash
kill [signal] PID killall [signal] process_name
Example
This example shows how to find a process ID and kill it politely, then force kill if needed.
bash
# Find the PID of the process named 'gedit' pid=$(pidof gedit) # Kill the process politely kill $pid # If still running, force kill kill -9 $pid
Common Pitfalls
Common mistakes include:
- Using
killwithout the correct PID. - Not having permission to kill a process (may need
sudo). - Using
kill -9immediately without trying polite stop first.
Always try to stop a process gently before forcing it.
bash
## Wrong way: force kill immediately kill -9 1234 ## Right way: polite kill then force if needed kill 1234 sleep 2 kill -9 1234
Quick Reference
| Command | Description |
|---|---|
| kill PID | Send default SIGTERM to stop process politely |
| kill -9 PID | Send SIGKILL to force stop process immediately |
| killall process_name | Kill all processes with the given name |
| pidof process_name | Find PID(s) of a process by name |
Key Takeaways
Use
kill PID to stop a process politely by its ID.Use
killall process_name to stop all processes by name.Try polite stop before force killing with
kill -9.You may need
sudo if you lack permission to kill a process.Find process IDs with
pidof or ps commands.