Process Management
Command Line
Operating Systems
Computer Programming
Troubleshooting

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.

Browse interview questions

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:

  • pgrep searches for processes based on name and other attributes and outputs their PIDs.
  • pkill can send signals to processes by name, partial name, and other attributes.

Example: Killing processes by partial name

Using pkill with a partial name:

bash
pkill -f your_partial_process_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:

bash
pkill -9 -f your_partial_process_name

Using ps, grep, awk, and kill

While pkill is straightforward and effective, understanding a more manual method can be beneficial for complex scenarios:

  1. List processes: ps aux is a command that lists all running processes with detailed information.
  2. Filter processes: Pipe ps aux output through grep to find lines containing the partial name.
  3. Extract PIDs: Use awk to extract the second column (PID) from the filtered list.
  4. Kill processes: Pass the PID to kill to send signals.

Complete example command:

bash
ps aux | grep your_partial_process_name | awk '{print $2}' | xargs kill

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 kill first 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

CommandUsageSignal TypeSafety
pkill -f nameSimple, directSIGTERM (default), SIGKILL with -9High, if used carefully
`ps aux \grep ...`Detailed, manualCustomizableMedium, requires careful handling
kill, kill -9Direct PID targetingSIGTERM, SIGKILLLow, 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.