How To Use Kill: The Complete Linux Command Guide
The “kill” command on Linux is a powerful tool that terminates processes running on your system. You’ll generally use it to stop a misbehaving application or to manage system resources efficiently. I’ve summarized the main options in this article.
The “kill” command allows users to terminate, interrupt, pause, or resume processes by specifying the process identifier on Linux.
I’ll start with the main syntax, and then show you examples and tips. Feel free to use the table of contents below if you already know the basics.
Command syntax
The basic syntax of the “kill” command is straightforward:kill <options> <PID>
In this syntax, we have two main parameters:
- PID refers to the process identifier. Each process running on your system has a number assigned to it. Using this number tells the system which process you want to stop.
- The options in this command are mostly used to decide which signal to send to stop the process (more on that later).
Usage examples
How to find the PID for a Linux process
Before using the “kill” command, find the PID of the process you want to terminate.
You can do this using various methods, such as the `ps` command or `pgrep.`
For example:ps aux | grep <process name>
orpgrep <process name>
After running the `ps` command, you will see the PIDs that match the process name you searched for. In the example above, Nginx is running 3 processes with PIDs 782, 783, and 785.
Once you have these processes’ IDs, you have everything you need to use the command “kill” with the syntax given above.
Terminating a single process
In most cases, you’ll use “kill” to stop a single process, only adding its ID in the parameter.
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
To terminate a single process, simply use the kill command followed by the PID for that process. For instance:kill 4941
This command sends the default termination signal (SIGTERM) to the process with PID 4941.
Terminating multiple processes
You can also terminate multiple processes at once by specifying multiple PIDs:kill 4941 2215 331
This command terminates processes with PIDs 4941, 2215, and 331.
Note: If you find several PIDs when using the “ps” command, they may be linked together, so killing one may stop all of them. For example, a bash script often generates child processes that will be stopped automatically when you kill the parent.
Main options
The kill command doesn’t have many options, as explained in the command syntax. The only useful one is to specify which signal is sent to the process when it is killed.
By default, kill sends the SIGTERM signal, which instructs the process to terminate. However, you can specify a different signal using the – option.
Some common signals include:
- SIGINT (Interrupt): This signal is often sent by pressing Ctrl+C in the terminal:
kill -SIGINT 331
- SIGKILL (Kill): This signal immediately terminates the process without any cleanup:
kill -SIGKILL 4941
- SIGSTOP (Stop): This signal pauses the process:
kill -SIGSTOP 2215
- SIGCONT (Continue): This signal resumes a stopped process:
kill -SIGCONT 2215
I included these signals in this article to be thorough, but I have rarely used them in my years as a sysadmin, so it’s probably no big deal if you don’t remember them.
Tips
While the kill command syntax is pretty simple, I still have a few tips that I want to share with you.
Terminating processes using sudo
First, you won’t necessarily be able to kill any process you want with your current user.
Some processes may require administrator privileges to terminate.
In such cases, if you try to terminate the process without root privileges, you will get an “Operation not permitted error.” That’s typically the case for system services, scheduled tasks and other applications started by other users on your system.
To bypass this, use sudo:sudo kill 5574
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
Sending kill signals to process groups
On Linux, a process group is a collection of one or more processes that are identified together.
You get the process group ID similarly to the process identifiers, with the “ps” command:ps -eo pid,pgid,cmd
You can then use the process group ID to terminate all the processes in the process group.
You do this by specifying a negative PID:kill -TERM -5658
Graceful vs. forceful termination
One last thing you must know about the “kill” command is the difference between stopping a process “gracefully” and “forcefully”:
- Graceful termination allows a process to perform cleanup tasks (such as saving data) and exit in an orderly manner. Graceful termination is initiated by sending the SIGTERM signal.
- Forced termination, on the other hand, kills a process immediately without allowing it to clean up. Forced termination is done by sending the SIGKILL signal.
You should perform graceful termination whenever possible (the default option). Forceful termination should only be used when processes are unresponsive.
Related questions
What is the difference between SIGTERM and SIGKILL?
SIGTERM allows processes to perform cleanup tasks before termination. SIGKILL immediately terminates the process without any cleanup.
How do I send a signal to a specific user’s processes?
You can use the `pkill` command with the -u option to send signals to processes owned by a specific user:pkill -u <username>
Can I terminate a process by name instead of PID?
Yes, you can! Again, you make use of the `pkill` command and pass to it the name of the process you want to terminate:pkill firefox
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
Going further
The “kill” command offers even more functionality. For example, you can send custom signals or manage process priorities.
This is pretty advanced stuff, but you can get further details and a comprehensive list of command options by looking up the “kill” manual by running:man kill
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
Reminder: Remember that all the members of my community get access to this website without ads, exclusive courses and much more. You can become part of this community for as little as $5 per month & get all the benefits immediately.
Additional Resources
Overwhelmed with Linux commands?
My e-book, “Master Linux Commands”, is your essential guide to mastering the terminal. Get practical tips, real-world examples, and a bonus cheat sheet to keep by your side.
Grab your copy now.
VIP Community
If you just want to hang out with me and other Linux fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.
Need help building something with Python?
Python is a great language to get started with programming on any Linux computer.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.