How to Kill Process in Linux? | Explained with Examples

How to kill process in linux explained with examples Thumbnail

Several processes are running simultaneously in the daemons of Linux, which we want to kill. Most of the time, we must be aware of the processes running in the daemons that use the computer’s RAM. We can stop these processes by killing them in Linux.

In this blog, different methods of killing the processes are discussed by all the possible methods in Linux. This blog will cover the following sections:

  • How to Kill Process in Linux?
  • Kill Command in Linux with Examples
  • Kill a Linux Process Using PID
  • Kill Multiple Linux Processes Using PID
  • Kill All the Linux Processes
  • Kill Linux Process by Process Name
  • Kill Linux Processes by User Name
  • Force Kill Process in Linux
  • Kill Linux Process on a Specific Port
  • Kill Linux Group Processes
  • How to Show Warning Before Killing a Process?
  • How to Kill the Process Using Top Command?

So let’s start the blog!

How to Kill Process in Linux?

The “kill” command is used to kill unnecessary processes in Linux. We can use the Process ID, Process Name, and User Name to kill the processes with the kill command.

The general syntax of the kill command is as follows:

$ kill [signal] Process

Kill: The kill specifies the kill command utility is used.

Signal (optional): There are three types of signals

  • SignalNumber (i.e., 9) The default value is
  • SignalName (i.e., kill) The default signal is “kill”.
  • SignalName with SIG (i.e., SIGkill)

Process: The process can be specified with PID, Process Name, or User Name.

Kill Command in Linux with Examples

The kill command in Linux is used for killing/terminating a process or several processes using the “kill” command. Using the below commands, we can kill the processes in different ways:

Command How the Process will be killed
$ kill PID This command kills a process using the process ID.
$ kill PID1 PID PID3… To kill multiple processes using their PID.
$ killall ProcessName This command kills a process utilizing Process Name.
$ killall ProcessName1 ProcessName2… Kill several processes using the Process Names.
$ killall -v ProcessName To check the details of the killing process/processes, use this command.
$ killall -u UserName To kill the processes using the User Name.
$ pkill ProcessName For killing a single process, use the Process Name.
$ kill -9 PID This command forcefully kills a process.
$ kill -9 ProcessName For killing a process by force, using its Process Name.
$ sudo kill -9 $(sudo lsof -t -i:<PortNumber>) This command kills the process for a specific port number.
$ kill — -<ProcessGroupID> Run this command in the terminal to kill a process using the process group ID.
$ killall -i <ProcessName> To show a warning message before killing the processes.

Let’s discuss methods to kill the process/process in Linux with examples.

Kill a Linux Process Using PID

Processes are mostly killed using their Process ID. To kill a process by PID, use the below command:

Note: To find the Process ID (PID) of a specific task, use the “pidof <ProcessName>”.

$ kill PID

kill a process using PID

Kill Multiple Linux Processes Using PID

The “kill” command kills several processes simultaneously to increase the processing speed. To kill three processes with PIDs 3423, 3318, and 546, use the below command:

$ kill pid1 pid1 pid3
$ kill 3423 3318 546

kill multiple processes using PID

Note: You can get a Linux Kill command error “operation not permitted”; you can use the “-9” flag to kill the process forcefully. If the error remains, you need “sudo” permissions to kill those processes.

Kill All Linux Processes

The kill command allows you to kill all the processes using the “killall” command. We can kill all processes using the below command:

$ killall <processname>

To kill all the processes of the “firefox web browser”, use the following command:

$ killall firefox

kill process using process names

The Firefox browser is shut down.

The “v” option of the “killall” command lets you get details about the processes. To kill all firefox processes with details, use the below command:

$ killall -v {ProcessName}

show details of killing processes

The kill details are shown in the output.

Kill Linux Process by Process Name

We can kill a process or multiple processes by its name. The kill all command is used to kill all the processes written within the command. For killing a single process, the “pkill” command is used.

The general syntax of killall and pkill commands are as follows:

$ killall <ProcessName>...
$ pkill <ProcessName>

To kill multiple processes like “vlc” and “firefox”, use the below command:

$ killall firefox vlc

kill multiple process using process names

To kill a single process, we use the “pkill” command. To kill a single process vlc, use the command:

kill a single process using process name

Kill Linux Processes by User Name

If you want to kill all the processes owned by another user, the “u” option of the killall command is used. To kill the processes owned by a specific user, the command used will be:

$ killall -u <UserName>

kill process using user name

Force Kill Process in Linux

If the process is not killing using the “kill pid” or “killall <processname>” commands, we can use the “9” signal of the kill command for force kill.

To force kill the process by its id, use the below command:

$ kill -9 PID

For instance: to kill the process with 1523 Process ID, use the below command:

$ kill -9 1523

force kill a process using pid

Similarly, to force kill all the processes using the process name, we can use the following command:

$ killall -9 <ProcessName>

To force kill the processes of firefox, execute the below command:

$ killall -9 firefox

force kill a process using process name

Kill Linux Process on Specific Port

Do you need to kill the process for the specific port in Ubuntu? Utilize the below command to kill the port process:

$ sudo kill -9 $(sudo lsof -t -i:<PortNumber>)

To kill a process with port number 6004, use the below command:

$ sudo kill -9 $(sudo lsof -t -i:6004)

kill a process using port name

Kill Linux Group Processes

The kill command can be used to kill the process of a specific group. To get the process group, the PID, Process Group ID, and the Process Name, use the below command:

$ ps x -o "%p %r %y %x %c"

get details about the processes

To kill the process of a specific group, use the below command:

$ kill -- -<ProcessGroupID>       =>to kill with process group processID
$ kill -9 -<ProcessGroupID>       =>to kill forcefully with group processID
$ pkill -9 -G $GID                =>to kill a single process with groupID

$ kill -TERM -- -<ProcessGroupID> =>to kill the process with GID

How to Show Warning Before Killing a Process?

If you want to avoid killing an important process, use the “i” option of the killall command. The syntax of the killall with permission command is written below:

$ killall -i <ProcessName>

To kill the processes of VLC by asking permission, use the below command:

Kill a process after confirming

Press “y” to kill the process or “N” to terminate the killing process.

How to Kill the Process Using Top Command?

1. Launch the system monitoring tool by running the “top” command in the terminal.

top command in Linux

The below system monitoring interface will open:

top command interface linux

2. Press the shortcut key “k” to indicate the tool to kill the process.

press k to enter the kill mode

3. Enter the PID of the process to kill and press the “Enter” key.

enter pid to kill the specific process

4. A new prompt will show to give the “Signal” id for the process. Enter the number “9” to give a signal to kill the process.

enter 9 to force kill a process

5. Press “Enter” to kill the process.

The process is successfully killed using the top command.

Conclusion

The kill command is used to kill processes in Linux. We can kill the process using Process ID (PID), Process Name, User Name, specific Port Number, Group ID, and the Top Command. Different ways to kill the process in Linux are discussed in this article.

Leave a Reply

Your email address will not be published. Required fields are marked *