1

So here's what I want to do: User inputs a USERNAME. Based on this username, I need to get the list of processes started by this user. I am planning to do this by getting the UID of this user and listing all the processes with this UID. I only found UID in the /proc/$PID/status file. I am unclear about how do I proceed with this.

3
  • A pgrep -u $USERNAME isn't enough? What else do you need from /proc/$PID/status? – muru Oct 15 '15 at 5:14
  • I want to get the UID of the user. Once UID is available, I need to fetch all the PIDs that are under this UID (Basically, get all the processes started by this user). pgrep lists all the PIDs of a particular user, but I need the UID too. – Dee Oct 15 '15 at 5:19
  • check this unix.stackexchange.com/questions/85466/… link. It will be helpful. – AVJ Oct 15 '15 at 5:42
3

To get the UID from the username, use id -u:

$ id -u root
0
$ id -u lightdm
112
$ id -u nobody 
65534

But you are re-inventing the wheel. pgrep already handles this just fine:

$ pgrep -u www-data
1909
1910
1911
1912
$ id -u www-data   
33
$ pgrep -u 33      
1909
1910
1911
1912

You can also use plain ps:

$ ps -U www-data -o uid,pid
  UID   PID
   33  1909
   33  1910
   33  1911
   33  1912
1
  • Got it, pgrep was more than enough! Thanks a lot! – Dee Oct 15 '15 at 5:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.