How to End Processes With kill, pkill, and killall

When working with a Unix-based operating system such as Linux or macOS, it is common to encounter situations where a process becomes unresponsive or needs to be terminated for some reason. Fortunately, there are several command-line utilities available that allow users to end processes in a variety of ways. In this article, we will explore three of the most commonly used utilities for terminating processes: kill, pkill, and killall. We will cover their basic usage, common options, and examples of how they can be used to manage processes on a Unix system.

Basic Usage of kill, pkill, and killall

Before diving into specific examples, it is helpful to understand the basic usage of each utility.

kill is a command-line utility that sends a signal to a process to terminate it. By default, kill sends the TERM signal (signal 15), which asks the process to gracefully terminate itself. If a process does not respond to the TERM signal, it can be sent the KILL signal (signal 9), which immediately terminates the process.

pkill is similar to kill, but it allows you to send signals to processes based on their name or other attributes. For example, you can use pkill firefox to terminate all instances of the Firefox web browser.

killall is similar to pkill, but it only matches processes by their exact name. This can be useful when you want to terminate all instances of a particular program.

Examples of Using kill

Let's start with some examples of using kill to end processes.

Terminating a Single Process by PID

The most basic usage of kill is to terminate a single process by its process ID (PID). To find the PID of a process, you can use the ps command:

ps aux | grep process_name

This will show you a list of processes that match the specified name. The second column of the output shows the PID of each process.

Once you have the PID, you can use the kill command to terminate the process:

kill pid

For example, to terminate a process with PID 1234, you would run:

kill 1234

This will send the TERM signal to the process, asking it to gracefully terminate itself. If the process does not respond, you can use the -9 option to send the KILL signal, which will immediately terminate the process:

kill -9 1234

Terminating Multiple Processes by PID

If you need to terminate multiple processes at once, you can pass multiple PID values to kill. For example, to terminate processes with PID 1234 and 5678, you would run:

kill 1234 5678

Terminating All Processes Belonging to a User

If you want to terminate all processes belonging to a particular user, you can use the -u option to specify the user's username. For example, to terminate all processes belonging to user "jdoe", you would run:

pkill -u jdoe

Note: The standard kill command does not support the -u option directly. Use pkill for user-based process termination.

Examples of Using pkill

Next, let's look at some examples of using pkill to terminate processes.

Terminating Processes by Name

The most basic usage of pkill is to terminate processes by name. For example, to terminate all instances of the Firefox web browser, you would run:

pkill firefox

This will send the TERM signal to all processes whose name contains "firefox". If any of these processes do not respond to the TERM signal, you can force termination using the -9 option:

pkill -9 firefox

Terminating Processes by Process Group ID

In addition to terminating processes by name, pkill can also terminate processes by their process group ID (PGID). The PGID is a unique identifier assigned to each process group by the operating system. To terminate all processes in a particular process group, you can use the -g option followed by the PGID:

pkill -g 1234

Terminating Processes by User

pkill also allows you to terminate processes based on the user that owns them. You can use the -u option to specify the user's username:

pkill -u jdoe

Examples of Using killall

Finally, let's look at some examples of using killall to terminate processes.

Terminating Processes by Name

The most basic usage of killall is to terminate processes by their exact name. For example, to terminate all instances of the Firefox web browser, you would run:

killall firefox

This will send the TERM signal to all processes whose name exactly matches "firefox". If any of these processes do not respond to the TERM signal, you can use the -9 option to force termination:

killall -9 firefox

Terminating Processes by User

Like pkill, killall also allows you to terminate processes based on the user that owns them. You can use the -u option to specify the user's username:

killall -u jdoe

Signal Options and Advanced Usage

So far, we have only discussed how to use these commands to terminate processes using the default signal (SIGTERM or 15). However, there are other signals that can be used to control processes. Here are the most commonly used signals:

Signal Number Description
SIGTERM 15 Graceful termination (default)
SIGKILL 9 Immediate termination (cannot be caught)
SIGHUP 1 Hangup signal (often used to reload configuration)
SIGINT 2 Interrupt signal (Ctrl+C)
SIGQUIT 3 Quit signal with core dump

You can use these signals by specifying their signal number or name as an option. For example, to terminate a process with the SIGKILL signal:

kill -9 <PID>
kill -KILL <PID>
pkill -9 firefox
killall -TERM firefox

Comparison of Commands

Command Target Method Matching Best Use Case
kill Process ID (PID) Exact PID Terminating specific processes
pkill Process name, user, group Pattern matching Terminating processes by criteria
killall Process name Exact name match Terminating all instances of a program

Best Practices

  • Always try SIGTERM (15) first to allow graceful shutdown before using SIGKILL (9).

  • Use ps aux or pgrep to verify process names and PIDs before terminating.

  • Be cautious when terminating system processes or processes owned by other users.

  • Consider using pgrep to preview which processes will be affected before using pkill.

Conclusion

The kill, pkill, and killall commands provide flexible ways to terminate processes on Unix-based systems. Each command serves different purposes: kill for specific PIDs, pkill for pattern-based matching, and killall for exact name matching. Understanding these tools and using them responsibly helps maintain system stability and process management.

Updated on: 2026-03-17T09:01:38+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements