The Wayback Machine - https://web.archive.org/web/20240717024616/https://www.geeksforgeeks.org/kill-command-in-linux-with-examples/
Open In App

How to Kill a Process in Linux | Kill Command

Last Updated : 26 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

kill command in Linux (located in /bin/kill), is a built-in command which is used to terminate processes manually. kill command sends a signal to a process that terminates the process. If the user doesn’t specify any signal that is to be sent along with the kill command, then a default TERM signal is sent that terminates the process.

Basic Syntax of kill command in Linux

The basic syntax of the `kill` command is as follows:

Syntax :

kill [signal] PID

Here, 

  • PID = The `kill` command requires the process ID (PID) of the process we want to terminate.
  • [signal] = We have to specify the signal and if we don’t specify the signal, the default signal `TERM` is sent to terminate the process

Signals can be specified in three ways:

Signals can be specified in three ways; they are as follows:

1. By number:

We can specify a signal using a number. For example, we have a PID `1212` and want to send a `SIGKILL` signal to kill this PID. SIGKILL has a signal number of `9` (To find signal numbers run `kill -l` command).

Syntax:

kill -9 1212

2. With SIG prefix (e.g/ -SIGkill)

We can also specify signal using SIG prefix. For example, we need to send a signal `SIGTERM` and PID is `1432`. To just check signal number of `SIGTERM` signal we can use `kill -l` command.

Syntax:

kill -SIGTERM 1432

3. Without SIG prefix:

We can also specify signals without using SIG prefix. For example, if want to send signal `TERM` and PID `1234`. To just check signal number of `TERM` signal we can use `kill -l` command.

Syntax:

kill -TERM 1234

Some Common Signals in kill Command

The table below shows some common signals and their corresponding numbers.

Signal Name Signal Number Description
SIGHUP 1 It hangup detected on controlling terminals or death of controlling process.
SIGINT 2 It interrupts from keyboard.
SIGKILL 9 It kills signal.
SIGTERM 15 It terminates signal.

To check signal name and number we can use `kill -l` command.

Options and Examples of kill Command in Linux

kill -l Option

To display all the available signals, you can use the below command option:

 Syntax:

kill -l
kill -l

kill -l

Note:

  • Negative PID values are used to indicate the process group ID. If you pass a process group ID then all the process within that group will receive the signal.
  • A PID of -1 is very special as it indicates all the processes except kill and init, which is the parent process of all processes on the system.
  • To display a list of running processes use the command ps and this will show you running processes with their PID number. To specify which process should receive the kill signal we need to provide the PID.

Syntax:

ps
ps

ps

kill PID Option

This option specifies the process ID of the process to be killed.

 Syntax:

kill pid

kill -s Option

This option specifies the signal to be sent to the process.

 Syntax:

kill {-signal | -s signal} pid 

kill Command in Linux -FAQs

How to Kill a Process from the Linux Command Line?

To kill a process from the Linux command line, you can use the kill command followed by the process ID (PID) of the target process. For example, to terminate a process with PID 1234, you would use the following command:

kill 1234

By default, the kill command sends the SIGTERM signal, allowing the process to perform cleanup operations before termination. If the process doesn’t respond to SIGTERM or if immediate termination is required, you can use the SIGKILL signal with the -9 option:

kill -9 1234

It’s important to note that forcefully terminating a process with SIGKILL may result in data loss or corruption, so it’s recommended to try SIGTERM first and resort to SIGKILL only if necessary.

How to kill multiple processes at once?

We can use kill command to kill multiple processes at once. We just need to specify multiple PIDs separated by spaces, or we can also use `killall` command to kill all the processes with a specific name.

For Example:

If we want to kill processes PIDs like 1234, 4321, and 2342, we can we use the following command:

kill 1234 4321 2342

If we want to kill all the processes with the name “firefox”. We can use the following command.

killall firefox

Is it safe to kill the system processes?

No, it is important to note that only kill processes that we know are safe to kill and killing system processes can cause system instability. It is always better to use other methods like restarting the system or identifying the root cause of the issue and fixing it.

What is the difference between SIGTERM and SIGKILL in `kill` command in Linux?

SIGTERM, it sends a termination signal to the process which helps in exit gracefully. Whereas SIGKILL sends a kill signal to the process, which terminate the processes forcefully and immediately.

For Example:

To send signal `SIGTERM` and PID `4321`, we can use the following command.

kill -15 4321

To send signal `SIGKILL` and PID `4321`, we can use the following command.

kill -9 4321

How can I find the process ID (PID) of a specific program in Linux?

To find the PID of a specific program, you can use the pgrep command followed by the program’s name. For example, to find the PID of the “firefox” process, you would enter:

pgrep firefox

What is the purpose of the pkill command, and how does it differ from the kill command?

The pkill command is used to send signals to processes based on their name, providing a more convenient way to terminate processes. It differs from the kill command by allowing users to specify processes by name rather than PID.

How can I check the resource usage (CPU, memory) of a specific process in Linux?

To check the resource usage of a specific process, you can use the top command along with the process ID (PID). For instance, to monitor the resource usage of a process with PID 1234, you would run:

top -p 1234

Conclusion

The `kill` command in Linux is a very powerful utility for managing processes. We have understood the different ways to specify the signals and available options in the kill command which can help us to manage our system resources efficiently and resolve issues quickly and effectively.  



Previous Article
Next Article

Similar Reads

kill all Command in Linux with Examples
Have you ever confronted the circumstance where you executed a program or an application, and abruptly while you are utilizing the application, it gets halted and starts to crash? You attempt to begin the application again, yet nothing happens on the grounds that the first application measure never genuinely closes down totally. The arrangement is
5 min read
Easy way to kill process using Fkill in Linux
fkill-cli is a command-line-based tool to kill processes interactively. Fkill is developed using Nodejs. It can run on different operating systems like macOS, Linux, and Windows. To kill the process using fkill we just have to mention the process name or the process PID. Fkill can also be used to kill port. Installation of fkill on Different OS. No
3 min read
How to kill processes on the Linux Desktop with xkill
xkill is a command-line utility that can kill the undesired windows on the user's screen. Basically, xkill force the X server to close the connection to the client. This utility kills the programs without providing PID with a command. Now let's see how to install the xkill on the systems. Installation of xkill The xkill utility comes with the packa
4 min read
How to Kill a Detached screen Session in Linux
The screen session is like virtual windows, And in Linux it works like a multiplexer terminal where we can create more than one window and work on that. we can create multiple windows inside one session. We called it virtual because the process will continue to run when their window will not be visible even if you have disconnected. Now, if we are
3 min read
How to Kill Processes by Given Partial Names in Linux
On a Unix system creates a separate environment for a program when it is executed. Everything the system needs to run the program as if there were no other programs in this environment. In Unix, each command you issue initiates or starts a new process. You initiated a process using the ls command to list the directory contents.Simply put, a process
4 min read
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
Linux Process Management Command Cheat Sheet
On Linux computers there are special commands to control the programs that are running. These commands are called process management commands. With the help of process management commands you can look at the list of the programs that are currently running on your computer. You can also start new programs and run them and stop programs that are alre
6 min read
select command in Linux with examples
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
1 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read