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

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


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'


You can parse out the /proc entry if you are on Linux and if you just need a numeric pid (or are OK with it). Here is an example for the mysqld process:

grep -e '^Uid:' /proc/$(pidof mysqld)/status | cut -f 2


The shortest way I found so far ( $PID - ID of the process inspected):

ps -p $PID -o euid=