How to find PID's user name in linux
Can you help me to find the PID's user name, Some time my server got high load. When i top -c, I cannot even find the PID's user name who / which is causing load on the server.
I'm surprised nobody has put this up yet:
Try the -p
option to the ps
command.
For instance, if you have PID 1234
, run:
ps -u -p 1234
(The -u
was added to include the username in the output)
You can the use grep
or awk
, etc. to extract the info you want.
/proc/processID/status
will have the information about user's ID which you can use to find the username.
This does the same:
uid=$(awk '/^Uid:/{print $2}' /proc/YOUR_PROCESS_ID/status)
getent passwd "$uid" | awk -F: '{print $1}'
Replace YOUR_PROCESS_ID with your process ID number.
Get only username from a PID:
PID=136323
USERNAME="$( ps -o uname= -p "${PID}" )"
You can also combine it with a pgrep
. In this example we show all usernames executing some .php
file:
pgrep -f '\.php' | xargs -r ps -o uname= -p | sort -u
Find only one username running a certain unique process:
USERNAME="$( pgrep -nf 'script\.php' | xargs -r ps -o uname= -p )