Get username and process id in bash

I am writing a bash script where I need to find out the process user id. For 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 userID 1000, but I need a username.

+9
source share
4 answers

This can happen if the username is longer than 8 characters (OR) . The identifier does not have a name. But, if you want the username to be psdisplayed, try this,

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

/proc, Linux, pid ( ). mysqld:

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

Here is an example for the gedit process

grep -w Pid /proc/$(pidof gedit)/status | cut -f 2
0
source

The shortest path I've found so far ($ PID - identifier of the process being checked):

ps -p $PID -o euid=
0
source

Source: https://habr.com/ru/post/1526535/


All Articles