Bash Script to Find and Kill Process by Name
Use
pkill -f process_name to find and kill a process by name in Bash, or use kill $(pgrep -f process_name) for more control.Examples
Inputprocess_name=firefox
OutputAll running Firefox processes are terminated.
Inputprocess_name=nonexistentprocess
OutputNo processes found; no action taken.
Inputprocess_name=python
OutputAll Python processes are terminated.
How to Think About It
To find and kill a process by name, first identify all running processes matching the name, then send a termination signal to each. This can be done by searching process names and using their process IDs to kill them.
Algorithm
1
Get the process name as input.2
Search for all process IDs matching the name.3
If no process is found, exit or notify.4
Send a kill signal to each found process ID.5
Confirm the processes are terminated.Code
bash
#!/bin/bash process_name="$1" if [ -z "$process_name" ]; then echo "Usage: $0 process_name" exit 1 fi pids=$(pgrep -f "$process_name") if [ -z "$pids" ]; then echo "No process found with name '$process_name'." exit 0 fi echo "Killing processes with name '$process_name':" for pid in $pids; do echo "Killing PID $pid" kill "$pid" done
Output
Killing processes with name 'firefox':
Killing PID 12345
Killing PID 12346
Dry Run
Let's trace killing 'firefox' processes through the code
1
Get process name
process_name='firefox'
2
Find PIDs
pids='12345 12346'
3
Kill each PID
kill 12345 kill 12346
| Step | Process Name | PIDs Found | Action |
|---|---|---|---|
| 1 | firefox | Read input | |
| 2 | firefox | 12345 12346 | Find PIDs with pgrep |
| 3 | firefox | 12345 | kill 12345 |
| 4 | firefox | 12346 | kill 12346 |
Why This Works
Step 1: Find process IDs
The script uses pgrep -f to find all process IDs matching the given name, including full command lines.
Step 2: Check if processes exist
If no matching processes are found, the script exits gracefully without errors.
Step 3: Kill each process
The script loops over each found PID and sends a kill signal to terminate the process.
Alternative Approaches
Using pkill
bash
#!/bin/bash process_name="$1" if [ -z "$process_name" ]; then echo "Usage: $0 process_name" exit 1 fi pkill -f "$process_name" && echo "Killed all '$process_name' processes." || echo "No process found."
Simpler and faster but less control over individual PIDs.
Using ps and grep
bash
#!/bin/bash process_name="$1" if [ -z "$process_name" ]; then echo "Usage: $0 process_name" exit 1 fi pids=$(ps aux | grep "$process_name" | grep -v grep | awk '{print $2}') if [ -z "$pids" ]; then echo "No process found with name '$process_name'." exit 0 fi for pid in $pids; do kill "$pid" done
More manual and less efficient, but works on systems without pgrep/pkill.
Complexity: O(n) time, O(n) space
Time Complexity
The script scans all running processes once to find matches, so time grows linearly with the number of processes.
Space Complexity
It stores matching process IDs in memory, which grows with the number of matches but is usually small.
Which Approach is Fastest?
Using pkill is fastest and simplest, while ps | grep is slower and more manual.
| Approach | Time | Space | Best For |
|---|---|---|---|
| pgrep + kill loop | O(n) | O(n) | Control over each PID |
| pkill | O(n) | O(1) | Quick kill by name |
| ps + grep + kill | O(n) | O(n) | Systems without pgrep/pkill |
Always check if the process name is provided and handle the case when no processes are found.
Beginners often forget to quote variables, causing errors with process names containing spaces.