我想获取某些进程的运行时间。这就是我正在做的事情
ps -ef | grep "python3 myTask.py" | awk '{print $2}' | xargs -n1 ps -p {} -o etime
我想获取 pid
ps -ef | grep "python3 myTask.py" | awk '{print $2}'
然后将这些传递给
ps -p {} -o etime
通过使用 xargs,但它不起作用。我明白了
error: process ID list syntax error
Usage:
ps [options]
Try 'ps --help <simple|list|output|threads|misc|all>'
or 'ps --help <s|l|o|t|m|a>'
for additional help text.
For more details see ps(1).
error: process ID list syntax error
Usage:
ps [options]
Try 'ps --help <simple|list|output|threads|misc|all>'
or 'ps --help <s|l|o|t|m|a>'
for additional help text.
For more details see ps(1).
我做错了什么?
您可以使用以下命令:
pgrep -f "python3 myTask.py" | xargs -i{} ps -p {} -o etime
pgrep
- 根据名称和其他属性查找或发出信号处理。
-f, --full
-
该模式通常仅与进程名称匹配。当设置 -f 时,完整的命令行是
使用过。
如需进一步阅读,请参阅
man pgrep
。
xargs
段中缺少的部分是 -i{}
,它为每个参数调用命令,而 {} 将被它替换。
-i[replace-str], --replace[=replace-str]
-
如果指定了replace-str,此选项是-Ireplace-str 的同义词。
如需进一步阅读,请参阅
man xargs
。
您必须向 xargs 提供
-I{}
来设置占位符;否则无法使用。
尽管如此,您的命令太复杂并且涉及太多中间步骤(以及竞争条件)。只需获取您的流程(包括经过的时间)并过滤您需要的行:
ps -eo etime,cmd | awk '/python3 myTask.py/{print $1}'
(不再
xargs
了)