![如何通过“COMMAND”列过滤“ps fp”的输出?](https://linux22.com/image/229120/%E5%A6%82%E4%BD%95%E9%80%9A%E8%BF%87%E2%80%9CCOMMAND%E2%80%9D%E5%88%97%E8%BF%87%E6%BB%A4%E2%80%9Cps%20fp%E2%80%9D%E7%9A%84%E8%BE%93%E5%87%BA%EF%BC%9F.png)
我有
pgrep -U herman | xargs --no-run-if-empty ps fp
但我想COMMAND
通过搜索进一步按列过滤sh scriptName.sh Argument1
答案1
pgrep
为你做这件事可能会更容易,
pgrep -U herman -f "sh scriptName.sh Argument1"
如果事情比这更复杂,您可以直接使用数字pgrep
传递来检查通过 /proc 可见的进程属性(这是相同的位置ps
和pgrep
检查,因此您甚至不会“尴尬”地这样做)。例如
#!/usr/bin/env zsh
# /bin/bash should work too, but haven't checked.
for candidate in $(pgrep -U herman); do
# skip any Python process
grep -q bin/python3 /proc/${candidate}/cmdline || break
# check whether this has any *.log file as stdin or stdout or stderr
for fd in /proc/${candidate}/fd/{0,1,2}; do
grep -q '.*\.log$' ${fd} && echo ${candidate} && break;
done
done
将是一种检查任何进程(Python 除外)已连接到其 std* 的文件的方法。