2

The command

ps -o time -p 21361

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.

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.

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.

4

2 回答 2

2

我不确定,但 ps 在这方面可能有些错误。在这里试试这个:

$ 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 把它们剪掉,然后剪掉:

$ 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
于 2011-08-14T15:34:28.783 回答
0

在 MacOS X(10.7,Lion)上,手册页说:

-S 通过将所有退出的子进程与其父进程相加来更改进程时间的计算方式。

因此,我能够使用以下方法获得输出:

$ ps -S -o time,etime,pid -p 305
     TIME     ELAPSED   PID
  0:00.12 01-18:31:07   305
$

-S但是,该输出与省略 ' ' 选项时并没有什么不同。

我试过了:

$ ps -S -o time,etime,pid -p 305
     TIME     ELAPSED   PID
  0:00.14 01-18:43:59   305
$ time dd if=/dev/zero of=/dev/null bs=1m count=100k
102400+0 records in
102400+0 records out
107374182400 bytes transferred in 15.374440 secs (6983941055 bytes/sec)

real    0m15.379s
user    0m0.056s
sys     0m15.034s
$ ps -S -o time,etime,pid -p 305
     TIME     ELAPSED   PID
  0:00.14 01-18:44:15   305
$

/dev/zero如您所见,复制到的 15 秒系统时间/dev/null未包含在摘要中。

在这个阶段,弄清楚“ -S”选项的作用(如果有的话)的唯一方法是查看源代码。您可以sumrusage在 FreeBSD 版本中查找,例如,在FreeBSD

于 2011-08-14T16:42:19.627 回答