Get the username and the process ID of a process in bash

user2897690 picture user2897690 · Feb 12, 2014 · Viewed 26.5k times · Source

I am writing a bash script where I need to find out the userID of a process. For an example let the process be bash itself.

I tried ps aux | grep ba[s]h but the following was returned:

1000      2745  0.0  0.1  28360  5440 pts/1    Ss   10:11   0:01 bash

I see the userID 1000 displayed, but I want the username.

Answer

sat picture sat · Feb 12, 2014

This can happen if the username is longer than 8 characters (OR) id has no name. But, If you want the username in the ps output then try this,

ps -eo uname:20,pid,pcpu,pmem,sz,tty,stat,time,cmd | grep '[b]ash'