当我做ps aux | grep mongod时,我得到
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但是,我只对输出第三个条目的user感兴趣,该条目运行实际的mongod进程。我希望输出是公正的
mongod
我将如何调整ps、grep和awk来做到这一点?
发布于 2018-05-21 18:37:07
将其输送到awk并进行搜索:
ps aux | grep mongod | awk '/bin\/mongod /{print $8}'这样,您就可以放弃grep,只需让awk执行搜索:
ps aux | awk '/bin\/mongod /{print $8}'这将搜索记录中任何位置的字符串"bin/mongod“,然后返回该记录的第8位中的任何内容。
发布于 2018-05-21 18:38:54
尝试使用shell命令获取该用户很可能会崩溃。可以使用PID选项启动mongod吗?
/var/lib/mongodb-mms-automation/mongodb-linux-x86_64-3.4.13/bin/mongod -f /data/mdiag/data/automation-mongod.conf --pidfilepath /run/mongodb-pid.txt然后,只需运行ps $(cat /run/mongodb-pid.txt)就可以获得所需的特定进程。
发布于 2018-05-21 22:50:56
最好指定模式来匹配我们感兴趣的命令字段第一部分的末尾。此外,使用括号表达式代替\/,至少可以使我在查看匹配文件路径的模式时感到更高兴。
ps aux | awk -v command=11 -v user=1 '$command ~ /[/]bin[/]mongod$/ { print $user }'https://stackoverflow.com/questions/50454707
复制相似问题