How to Get PID of a Process in Linux Terminal
In this tutorial, we will look at some of the command line tools available at the bash shell that let us find the PID of a process in Linux.
To manage processes you need to know the PID of the process you want to manage. For example, let's say you want to terminate a process that is unresponsive. We can terminate a process using the kill command, but we need to know the process ID.
There are a couple of ways we can get the PID of a process in Linux. One method is to run the pgrep command.
Using the pgrep command
The pgrep command can be used to find PID of processes based on their name and other attributes. Its syntax is as follows:
For example, to find out the PID of the MySQL Server, execute the command as follows:
The pgrep command displays the process IDs of the processes that match the name specified on the command line.
For an exact match (find processes whose names exactly match the name) user the -x option.
pgrep -x mysqld
The pidof command is another bash command used for finding process IDs at the Linux shell.
However, unlike pgrep, pidof command requires the exact name of the process.
The preceding command outputs the process ID of the SSH Server.
The ps aux command with grep
If you don't know the exact name of the process, use ps aux command with grep to find a PID.
ps aux | grep -i process_name
For example, you can get the PID of the SSH server by running the following command in a Terminal window:
ps aux | grep -i ssh