Search code examples
awkgrepps

How to output just the user of a running process


When I do ps aux | grep mongod, I get

mongod    53830  0.1  0.3 247276 27168 ?        Sl   Apr04 128:21 /var/lib/mongodb-mms-automation/mongodb-mms-monitoring-agent-5.4.4.366-1.rhel7_x86_64/mongodb-mms-monitoring-agent
mongod   104378  0.6  0.8 469384 71920 ?        Ssl  Mar22 571:03 /opt/mongodb-mms-automation/bin/mongodb-mms-automation-agent -f /etc/mongodb-mms/automation-agent.config -pidfilepath /var/run/mongodb-mms-automation/mongodb-mms-automation-agent.pid >> /var/log/mongodb-mms-automation/automation-agent-fatal.log 2>&1
mongod   104471  0.6  5.4 1993296 433624 ?      Sl   Mar22 578:03 /var/lib/mongodb-mms-automation/mongodb-linux-x86_64-3.4.13/bin/mongod -f /data/mdiag/data/automation-mongod.conf

However, I'm only interested in outputting user of the third entry, which runs the actual mongod process. I'd like the output to be just

mongod

How would I tweak around ps, grep, and awk to do this?


Solution

  • Pipe it to awk and search:

     ps aux | grep mongod | awk '/bin\/mongod /{print $8}'
    

    With that you can probably drop the grep and just let awk do the searching:

     ps aux |  awk '/bin\/mongod /{print $8}'
    

    This is searching for the string "bin/mongod " anywhere in the record and then returning whatever is in the 8th position for that record.