Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

users and there processes

  • 10-09-2007 04:54PM
    #1
    Closed Accounts Posts: 91 ✭✭


    I am trying to write a script to list all users and their processes. I cant seem to find out how to link the PID to the user. For example if i run ps -a I get a nice list of all the processes running and their PID, but no information about which user is running them. I need the users real name not login in name so Joe Bloggs instead of joe.bloggs

    The output should look similar to this.


    Joe Bloggs
    PID TTY TIME CMD
    31799 pts/3 00:00:00 vim
    31866 pts/3 00:00:00 vim
    2495 pts/7 00:00:00 vim
    8368 pts/0 00:00:00 vim
    9544 pts/2 00:00:00 ps

    Henry Hill
    PID TTY TIME CMD
    8368 pts/0 00:00:00 vim
    9544 pts/2 00:00:00 ps

    anyone any ideas


Comments

  • Technology & Internet Moderators Posts: 28,859 Mod ✭✭✭✭oscarBravo


    Have you tried "man ps"?


  • Closed Accounts Posts: 91 ✭✭magnia


    yes, u any ideas. ive tried all the options that the man ps showed me. but i still dont know how i am going to get the username to appear like Joe Bloggs instead joe.bloggs


  • Technology & Internet Moderators Posts: 28,859 Mod ✭✭✭✭oscarBravo


    Users' real names are in /etc/passwd.


  • Closed Accounts Posts: 91 ✭✭magnia


    hmmm so could i do something like ps -aux|grep /etc/passwd


  • Registered Users, Registered Users 2 Posts: 1,193 ✭✭✭liamo


    Off the top of my head :
    1. cat the contents of /etc/passwd through awk to retrieve the username and comment (usually containing the real name) fields.
    2. iterate through the result of 1
    3. for each iteration of 2, count the processes for each user by piping a filtered list of processes (ps -u) into wc -l
    4. if the result of 3 is > 1 then output the real name and the result of ps -u <username>

    Probably about 10 lines of script to accomplish the above.
    The actual scripting I leave to you ;)

    Regards,

    Liam


  • Advertisement
  • Closed Accounts Posts: 181 ✭✭deadfingers


    Try prstat


  • Closed Accounts Posts: 91 ✭✭magnia


    yup catted the file then piped it and mess around another bit and finally got what i was looking for wooh


Advertisement