How to kill all processes with a given partial name?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
When working on a Unix-like system, such as Linux or macOS, it's common to need to manage system processes, including occasionally killing processes based on their names. This guide explores various ways to identify and kill all processes that match a given partial name using command-line tools like ps, grep, awk, pkill, and kill.
Understanding Processes and Signals
Before delving into the commands, it's crucial to understand that a 'process' in Unix-like systems is an instance of a running program. Each process is assigned a unique identifier called a Process ID (PID). To stop a process, the system uses signals, with the most common for terminating processes being:
SIGTERM(signal 15): Gracefully terminates the process. It's the default and safest way to kill a process.SIGKILL(signal 9): Kills the process immediately. This signal can't be caught or ignored and should be used as a last resort.
Using pgrep and pkill
pgrep and pkill are powerful utilities:
pgrepsearches for processes based on name and other attributes and outputs their PIDs.pkillcan send signals to processes by name, partial name, and other attributes.
Example: Killing processes by partial name
Using pkill with a partial name:
This command sends the SIGTERM signal to all processes whose command line arguments contain the substring "your_partial_process_name". Use -9 to send SIGKILL if necessary:
Using ps, grep, awk, and kill
While pkill is straightforward and effective, understanding a more manual method can be beneficial for complex scenarios:
- List processes:
ps auxis a command that lists all running processes with detailed information. - Filter processes: Pipe
ps auxoutput throughgrepto find lines containing the partial name. - Extract PIDs: Use
awkto extract the second column (PID) from the filtered list. - Kill processes: Pass the PID to
killto send signals.
Complete example command:
This chain of commands sends the default SIGTERM signal. Replace kill with kill -9 to forcefully stop the processes.
Best Practices and Safety Considerations
- Verify before killing: Use commands without
killfirst to ensure you're targeting the right processes. This can prevent accidentally stopping crucial system processes. - Understand the process: Know what a process does before killing it. Some processes might be critical to system or application stability.
- Use precise names: Be as specific as possible with the partial names to avoid unintentional matches.
Summary Table
| Command | Usage | Signal Type | Safety | |
pkill -f name | Simple, direct | SIGTERM (default), SIGKILL with -9 | High, if used carefully | |
| `ps aux \ | grep ...` | Detailed, manual | Customizable | Medium, requires careful handling |
kill, kill -9 | Direct PID targeting | SIGTERM, SIGKILL | Low, depends on PID accuracy |
By understanding these tools and methods, you can effectively manage processes on your Unix-like systems, enhancing both system performance and your productivity as a power user or system administrator.
Related reading
- How to kill off a pending APM operation
- How to kill pods on Kubernetes local setup
- How to kill tensorboard with Tensorflow2 jupyter, Win
- How to know if a point-to-point network is synchronous?
- How to know more details about a previous rollout revision using kubectl?
- How to list Kubernetes recently deleted pods?
- How to locate fuslogvw.exe on my machine?
- How to log formatted message, object array, exception?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.