如何在 Unix/Linux 命令 ps 中使用 S-output-modifier?
命令
ps -o time -p 21361
作品;但是我需要的是进程的运行时间,包括 all孩子们.比如21361是一个bash脚本,调用其他脚本,那么我想要总的运行时间,包括所有孩子的运行时间.
works; however what I need is the running time of the process including all the children. For example, if 21361 is a bash script, which calls other scripts, then I want the total running time, including the running time of all children.
现在 ps 文档列出了OUTPUT MODIFIER":
Now the ps documentation lists the "OUTPUT MODIFIER":
S
Sum up some information, such as CPU usage, from dead child processes into their parent. This is useful for examining a system where a parent process repeatedly forks off short-lived children to do work.
听起来恰到好处.不幸的是,没有 ps 语法的规范,所以我不知道把S"放在哪里!几个小时以来,我尝试了很多组合,但是要么我得到语法错误,要么S"什么也没做.而在互联网上你只能找到关于 ps 的非常基本的信息(并且始终相同),特别是S"修饰符我在任何地方都找不到提及,也没有人解释过 ps 的语法.
Sounds just right. Unfortunately, there is no specification of the ps-syntax, so I have no clue where to place the "S"! For hours now I tried many combinations, but either I get syntax errors, or "S" makes nothing. And on the Internet you find only very basic information about ps (and always the same), specifically the "S" modifier I couldn't find mentioned anywhere, and also nobody ever explains the syntax of ps.
我不确定,但 ps 在这方面可能有点问题.在这里试试这个:
I am not sure, but it might be that ps is somewhat buggy in this respect. Try this here:
$ ps p 12104 k time
PID TTY STAT TIME COMMAND
12104 ? Ss 16:17 /usr/sbin/apache2 -k start
$ ps p 12104 k time S
PID TTY STAT TIME COMMAND
12104 ? Ss 143:16 /usr/sbin/apache2 -k start
这是使用 ps 的 BSD 选项.它适用于我的机器,但是你会得到一个额外的标题行和额外的列.我会使用 tr 和 cut 将它们切掉:
This is using the BSD options for ps. It works on my machine, however you get an extra header row and extra columns. I would cut them away using tr and cut:
$ ps p 12104 k time S | tail -n 1 | tr -s '[:space:]' | cut -d ' ' -f 4
143:39
$ ps p 12104 k time | tail -n 1 | tr -s '[:space:]' | cut -d ' ' -f 4
16:17