Find Process Name from its PID
If you know the process ID (PID), you can get the process name using the ps command:
ps -p PID -o comm=
The ps command is used for process related operations. In the above command, -p PID
provides the process ID and -o comm=
asks it to output the command associated with the given PID.
Here's an actual example:
Honestly, it's difficult to remember the weird ps -p PID -o comm=
syntax. And if that's the case, you may either look into the man page of ps command or use other ways of getting the details.
You can list all running process using the ps command or top command and note the process ID and process name as desired.
If you know the PID, you can simply use the grep command to filter the output and get the details on that PID:
ps aux | grep PID
But as illustrated by the image below, the output gives other details along with the process name.
That's not an issue if you are manually reading it. However, if you have to use it in a script, it could be an issue.
This is where the ps -p PID -o comm=
is better.
Bonus tip: Since we are talking about process name and PIDs, let me quickly show you the reverse method, i.e., to find PID from process name.
There is a dedicated command called pidof
and you can use it like this if you know the exact process name:
pidof exact_process_name
So, you just learned how to find process name from its PID in Linux command line and you also learned to get the PID from the process name.
If something isn't working right, or you just have any comments in general, feel free to leave any of it in the comments below.