如何将 S-output-modifier 与 Unix/Linux 命令 ps 一起使用?

发布于 2024-11-29 15:29:27 字数 574 浏览 2 评论 0原文

该命令

ps -o time -p 21361

有效;但是我需要的是进程的运行时间,包括所有 孩子们。例如,如果21361是一个bash脚本,它调用其他脚本, 然后我想要总的运行时间,包括所有孩子的运行时间。

现在 ps 文档列出了“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 的语法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只怪假的太真实 2024-12-06 15:29:27

我不确定,但 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 和 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

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

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
衣神在巴黎 2024-12-06 15:29:27

在 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 复制到 /dev/null 所花费的 15 秒系统时间并未包含在摘要中。

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

On MacOS X (10.7, Lion) the manual page says:

-S Change the way the process time is calculated by summing all exited children to their parent process.

So, I was able to get output using:

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

However, that output was not really any different from when the '-S' option was omitted.

I tried:

$ 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
$

As you can see, the 15 seconds of system time spent copying /dev/zero to /dev/null did not get included in the summary.

At this stage, the only way of working out what the '-S' option does, if anything, is to look at the source. You could look for sumrusage in the FreeBSD version, for example, at FreeBSD.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文