ps axo : display full username based on PID number

Issue

I am trying to display full username by PID number

The first Approach i tried :

PID=12345
PID_USER=`ps -u -p ${PID}| awk '{print $1}' | tail -1`
echo $PID_USER

this will display as systemU+, i want to display as SystemUser_Farid

i have tried a second approach using ps axo , script used as

ps axo user:20,pid , however , this is printing the whole list of processes , using ps axo -p ${PID} user:20,pid doesnt show any result.

Solution

Use -o format for a user-defined format and only print the user name:

ps -o user= -p "$PID"

The = disables printing the header line.

Answered By – knittl

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published