How To Get PID Of A Process In Linux Command Line

If you want to kill a process using PID in Linux, you will have to get the PID of the process in the Linux command line. In this tutorial, we will see ways to find the process ID and then kill it via the command line.

How To Get Pid Of A Process In Linux Command Line

PID is the operating system’s unique identifier number for active programs that are running. We can say that the PID is a unique number assigned to each process when it is created and is used to identify the process in the operating system.

The the process ID (PID) of a running processes are stored in .pid file. The .pid file is usually located in the /var/run or /var/run/ directory and is named after the process it represents.

Knowing PID is very crucial in situations where a program has frozen or become unresponsive. In this condition you will not be able to close the program as it is not responding.

In such instances you can initiate the kill command via terminal and terminate the unresponsive program. In order to kill the said program we must know its process id or PID because kill command is always executed together with the PID as command argument.

Not only for the unresponsive applications, a user may need to kill the process to free up system resources.

ps command

The ps commnd is used to report a snapshot of the current processes. In simple words, ps displays information about a selection of the active processes.

By default, ps selects all processes with the same effective user ID (euid=EUID) as the current user and associated with the same terminal as the invoker.

The ps command displays the process ID (pid=PID), the terminal associated with the process (tname=TTY), the cumulated CPU time in [DD-]hh:mm:ss format (time=TIME), and the executable name (ucmd=CMD).

Also note that the ps command is output is unsorted by default.

To display a list of all running processes along with their PID (process ID) and other information you can use the ps -ef command.

ps -ef

There are various command usage of ps command to see every process on the system using standard syntax. You can use the following command syntax:

ps -e
ps -ef
ps -eF
ps -ely

Find a specific process using ps and grep command

To find a specific process, you can use the ps command together with the grep command to refine the output.

It is very useful in sorting the ps result and get the pid of the exact process. Using the command is very easy, for example, if you want to find a process named “firefox”, you can use the following grep command:

ps -ef | grep firefox

The command will then display all the processes that contain the word “firefox” in their name. You can see the screenshot below:

Find a specific process using pidof command

The pidof command is used to find the process ID of a running program. Pidof finds the process id’s (PIDs) of the named programs and displays it on the screen.

In the above command output {screenshot} you may see more than one process IDs because it displays all PIDs – including parent and child against the specified process name {given along the command as argument/option}.

If you know the name of the process, you can use the command pidof in this following manner:

pidof program_name

The above command is very useful get the process ID if you know the exact process name.

Bonus Tip: There is a command known as pgrep which combines the features ps and grep command.

Author: Team Linux Ledger

The team of Linux experts and open source enthusiasts who are passionate about sharing their expertise with you through in-depth tutorials, news, and updates on various aspects of open source in general and Linux in particular A team that helps you become a better Linux user.

Leave a Comment