How to Find the Process ID of a Program and Kill it?

In Linux, every running program is assigned a unique decimal number for its work called Process ID (PID). A process ID is assigned automatically to a program when it’s created, the system tracks the processes with this. Do you want to close an unresponsive process on Linux? Yes, there’s a method to do so. You can close the program by killing the process ID associated with it.

In this guide, we will discuss how to find the process ID of a program and kill it according to this timeline:

Method 1: Find Process ID Using pidof Command

The easiest way to find the program’s process id is using the “pidof” command in Ubuntu 22.04. The general syntax of the command is:

$ pidof <program_name>

Let’s test this command on Firefox Web Browser.

Note: Ensure that the Firefox Web Browser runs at the time of command execution.

$ pidof firefox

It provides you with the process IDs of the running processes for a specific program (firefox here).

Method 2: Find Process ID Using the pgrep Command

Another way to find the program’s Process ID is by using the “pgrep” command. Let’s find the Process ID of firefox using this command:

$ pgrep firefox

A single process ID of the firefox web browser is shown in the output.

Method 3: Find Process ID Using the “ps aux” Command

To find the process ID of the file with the list of open files, “ps aux” command. It manages all the processes in the Linux system, but we will use the ps aux command with “grep <program_name>” to get the list of our desired program processes.

$ ps aux | grep firefox

The list of processes in firefox with their PIDs are listed after executing the command.

Method 4: Find Process ID Using pstree Command

If you want to find the Process ID using the “pstree” command, utilize the below-mentioned command:

$ pstree -p | grep firefox

Firefox with its processes (PIDs) are shown in the above output.

Displaying Parent Process ID only using pstree Command:

For showing the ID of the parent process only, you can execute the below-mentioned command:

$ pstree -p | grep firefox | head -1

The output displays the PID (5911) for firefox.

How to Kill the Process Using PID in Linux?

Sometimes, we need to kill the processes that are unnecessary for us. There are multiple ways to kill a process in Linux, discussed below:

Example 1: Kill Single Process Using PID

After finding the desired Process ID, execute the below-mentioned command using its ID: 

$ sudo kill -9 <process_id>

Example 2: Kill Multiple Processes Using PID

If multiple processes are running and you want to close them at once, execute this command:

$ sudo kill -9 pid_1 pid_2

Both processes are closed using their identification IDs.

How to Kill Complete Program in Linux?

If you are not using a program and want to kill a program, you can do it in Linux by following multiple ways described below:

Example 1: Kill Complete Program Using PID

If you know the name of the program you want to close, you can close that by simply running the below-written command:

$ sudo kill -9 `pidof programe_name`

Firefox application was closed after the command execution.

Example 2: Kill All Processes in a Program

If you want to close an unresponsive program, you can kill all its process using a single command:

$ sudo killall program_name

Program is closed with all its related processes.

Conclusion

Process ID of a program can be found with “pidof <program_name>” and different methods elaborated in the guide. Besides this, You can kill a single process, multiple processes, or all the processes at once. You can use the “sudo kill -9 <program_id>” command to kill single or multiple processes using multiple IDs. In this guide, you have learned how to find the Process ID of a program and kill it with different methods.