33

Can you help me to find the PID's user name?

Sometimes my server has high load. When I run top -c, I cannot even find the owner of a process which is causing load on the server.

Aulis Ronkainen
  • 2,612
  • 42
  • 29
  • 27
Ranjithkumar T
  • 475
  • 1
  • 5
  • 6
  • We were experiencing server load issue due to bulk php process, so that i had this question, we can then find them using 'lsof -p xxxx'. – Ranjithkumar T Jan 14 '16 at 06:22

6 Answers6

56

I'm surprised nobody has put this up yet:

Try the -p option of 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 then use grep or awk, etc. to extract the info you want.

Matthias Braun
  • 1,162
  • 1
  • 17
  • 29
jwd
  • 3,562
  • 2
  • 22
  • 15
  • 1
    You were a tick faster than me. You're waking up earlier? Depending on the Linux distrbution, `ps u 1234` (Debian) or just `ps 1234` (Android with Busybox) also works. – ott-- Jan 10 '16 at 07:02
  • This works nicely with [`pgrep`](https://www.man7.org/linux/man-pages/man1/pgrep.1.html) when you only have the process name (not the PID) or when you want to see the owners of multiple processes with a similar name: `ps -u -p $(pgrep yourProcessName)` – Matthias Braun May 04 '23 at 18:42
8

/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.

u1686_grawity
  • 426,297
  • 64
  • 894
  • 966
P.P
  • 516
  • 4
  • 8
7

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 )
3

I think the shortest way is:

id -nu </proc/<pid>/loginuid

The /proc/<pid>/loginuid file has the uid number of the user running the process; id -nu reads uid from stdin and returns a user name.

Diego
  • 201
  • 2
  • 3
  • Nice. Is there a file of uid to user names that you can map the loginuid to a string user name? – Cory Robinson Dec 05 '19 at 14:11
  • Good answer, one of the best, but needs correction a bit. This _will_ work (bash example): `id -nu $(< /proc//loginuid)` (to work with any POSIX shell sholud be escaped with backticks instead of "$( )" bash construction). The answer with `ps` is more flexible, you can get also _real_ and _saved_ user name. But this answer command is several times faster than `ps --no-headers -o euser -p `. – dess Oct 11 '21 at 13:58
1

What do you want exactly? On my system, if I run 'top -c' I get:

  PID USER      PR  NI  VIRT  RES  SHR S   %CPU %MEM    TIME+  COMMAND                                                                                                                        
  2873 matt      20   0 3022m 1.6g 1.6g S     22 21.6   2245:42 /usr/lib/virtualbox/VirtualBox --comment ESX5-1 --startvm 4fd78ee9-739a-4d53-a0ce-4f9819ab9411 --no-startvm-errormsgbox        
  29764matt      20   0 2779m 1.4g 1.3g S      5 18.4 210:33.51 /usr/lib/virtualbox/VirtualBox --comment win2008-2 --startvm 202ec2b7-ae12-40e9-af76-2be429e553d7 --no-startvm-errormsgbox     
  17281root      20   0     0    0    0 S      2  0.0   0:05.90 [kworker/u:2]                                                                                                              

So the PID (processus/task identifier) is the first column, and the user account the processus runs under is the second column

mbarthelemy
  • 190
  • 4
0

You can use the following command.

use ps aux| grep "pid"

Also,

ls -ld /proc/"pid"/exe
Toto
  • 17,001
  • 56
  • 30
  • 41
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 13 '22 at 17:57