- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 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 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 TERM signal, which asks process to gracefully terminate itself. If a process does not respond to TERM signal, it can be sent KILL signal, which immediately terminates 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 Firefox web browser.
killall is similar to pkill, but it only matches processes by 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 PID of a process, you can use ps command −
$ ps aux | grep process_name
This will show you a list of processes that match specified name. second column of output shows PID of each process.
Once you have PID, you can use kill command to terminate process −
$ kill pid
For example, to terminate a process with PID 1234, you would run −
$ kill 1234
This will send TERM signal to process, asking it to gracefully terminate itself. If process does not respond, you can use -9 option to send KILL signal, which will immediately terminate 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 -u option to specify user's username. For example, to terminate all processes belonging to user "jdoe", you would run −
$ kill -u jdoe
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 Firefox web browser, you would run −
$ pkill firefox
This will send TERM signal to all processes whose name contains "firefox". If any of these processes do not respond to TERM signal, they will be sent KILL signal
Terminating Processes by Process Group ID
In addition to terminating processes by name, pkill can also terminate processes by their process group ID (PGID). PGID is a unique identifier assigned to each process group by operating system. To terminate all processes in a particular process group, you can use -g option followed by PGID. For example, to terminate all processes in process group 1234, you would run −
$ pkill -g 1234
Terminating processes by user
pkill also allows you to terminate processes based on user that owns them. You can use -u option to specify user's username. For example, to terminate all processes belonging to user "jdoe", you would run −
$ 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 name. For example, to terminate all instances of Firefox web browser, you would run −
$ killall firefox
This will send TERM signal to all processes whose name is "firefox". If any of these processes do not respond to TERM signal, they will be sent KILL signal.
Terminating Processes by User
Like pkill, killall also allows you to terminate processes based on user that owns them. You can use -u option to specify user's username. For example, to terminate all processes belonging to user "jdoe", you would run −
$ killall -u jdoe
Terminating Processes by Group
killall also allows you to terminate processes based on their group ID. You can use -g option to specify group ID. For example, to terminate all processes in group 1234, you would run −
$ killall -g 1234
Using kill, pkill, and killall with signal options
So far, we have only discussed how to use these commands to terminate a process using default signal (SIGTERM or 15). However, there are other signals that can be used to terminate a process. It is important to note that some signals can cause process to perform specific actions before terminating, such as saving data or cleaning up resources. Here are a few examples of signal options that can be used with kill, pkill, and killall −
SIGKILL (9) − This is most drastic signal and will immediately terminate process without allowing it to perform any cleanup actions. It is useful in situations where a process is unresponsive and needs to be terminated immediately.
SIGHUP (1) − This signal is commonly used to instruct a process to reload its configuration file. Some processes will also terminate when they receive this signal.
SIGINT (2) − This signal is typically sent to a process when user presses Ctrl-C. It instructs process to terminate gracefully.
SIGQUIT (3) − This signal is similar to SIGINT, but it also generates a core dump of process's memory for debugging purposes.
You can use these signals by specifying their signal number as an option to kill, pkill, or killall. For example, to terminate a process with SIGKILL signal, you would run −
$ kill -9 <PID>
Terminating Processes Based on Process Status
In addition to terminating processes based on their name, user, or group, you can also use killall to terminate processes based on their status. -s option allows you to specify status of processes you want to terminate. For example, to terminate all stopped processes, you would run −
$ killall -s STOPPED
Similarly, you can terminate all running processes using -s option with RUNNING status −
$ killall -s RUNNING
Terminating Processes with a Delay
Sometimes it is useful to give a process a chance to perform some cleanup actions before terminating it. For example, you might want to allow a process to save its current state or release some resources before terminating it. You can use kill command with -s option to send a signal to a process after a specified delay. syntax is as follows −
$ kill -s <SIGNAL> -<SECONDS> <PID>
For example, to send SIGTERM signal to a process with a delay of 10 seconds, you would run −
$ kill -s TERM -10 <PID>
This will give process 10 seconds to clean up before it is terminated.
Conclusion
In this article, we have explored three command-line utilities for terminating processes on a Unix system: kill, pkill, and killall. We have covered their basic usage, common options, and examples of how they can be used to manage processes. By understanding how to use these utilities, you can gain greater control over your system and handle unresponsive processes more effectively. It is important to use caution when terminating processes, as doing so improperly can lead to data loss or system instability. As always, it is a good practice to back up important data and to consult official documentation or seek expert advice if you are unsure about consequences of terminating a particular process.