Search code examples
linuxbashshellterminalprocess

How to get full username of process owner using ps


the title really says all I want to do, but there is a problem:

If I run ps aux | grep someRegexCondition I find the process I am interested in, but in the first column, where it tells me the process owner, it shortens the username, because it is too long.

It works fine, if the owner is root or someone with a shorter username, but in this case the output looks something like this (full username: myLongUsername):

myLongUserna+ PID 0.0 0.0 ...

I tried matching the part of the username that I get from that output with /etc/passwd, but while that works for me, there could be someone user named "myLongUsernameA", which could lead to problems. I guess it would be possible to then take the PID and get the owner with ps -o user= -p "PID", but is there a way to make the username column wider to show the full username?


Solution

  • Depending on your ps, you can use the :<width> specifier to make any column wider. For example, ps -axwwo uname:32,command will show the username as a 32-character-wide column, followed by an unlimited-length command (procps-ng ps). If you (for whatever reason) require an unlimited-length username, you could always put it last (e.g. ps -axwwo command:32,uname.

    For output resembling ps -u, for example, you can use -o uname:32,pid,%cpu,%mem,vsz,rss,tty,stat,start,time,command.