Linux / Bash 使用 PS -f 获取特定 PID 以与 PS -f 不同的格式返回,还有关于使用 Grep 解析此问题的问题

发布于 2024-09-25 09:06:09 字数 748 浏览 4 评论 0原文

对于我正在创建的 python 脚本,我需要首先获取进程的 PID(基于其名称),然后使用其 PID、持续时间(从打印输出中获取)从该进程获取下面,将是“00:00:00”

root      5686     1  0 Sep23 ?        00:00:00 process-name

我用它来通过进程名称获取PID:

ps -ef |grep `whoami`| grep process-name | cut -c10-15

所以,这工作正常,我假设剪切参数(-c10-15)将普遍工作PID的位置不应该改变(我刚刚从我发现的片段中得到这个)

但是,当我尝试做类似的事情来获取时间值时,例如它返回的

ps -f 5686

结果不同:

root      5686     1  0 Sep23 ?        S      0:00 /path/to/process

所以当我尝试剪切时,如下所示,我认为它不会正常工作,因为我不确定此返回上的间距是否一致,而且它显示的时间值与以前不同(不是原始的“00:00:00”样式的打印输出) 我在我的 python 脚本中使用它

ps -f 5686 | cut -c40-47

来记录特定进程类型(按名称)的 PI​​D,以便我以后可以在需要时关闭该程序的实例。赞赏

I have a need, for a python script I'm creating, to first get just the PID of a process (based on its name) and then to get from that process, usings its PID, its time duration, which, from the printout below, would be "00:00:00"

root      5686     1  0 Sep23 ?        00:00:00 process-name

I am using this to get just the PID, by the process's name:

ps -ef |grep `whoami`| grep process-name | cut -c10-15

So, this works fine and I am assuming that the cut parameters (-c10-15) would work universally as the placement of the PID shouldn't change (I just got this from a snippet I found)

However, when I try to do something similar to get just the TIME value, such as this it returns it differently

ps -f 5686

returns:

root      5686     1  0 Sep23 ?        S      0:00 /path/to/process

So when I try a cut, as below, I don't think it would work properly as I'm not sure the spacing on this return is consistent and also its showing the time value differently than before (not the original "00:00:00" style of printout.

ps -f 5686 | cut -c40-47

I'm using this in my python script to record the PID of a specific process type (by name) so that I can later shut down that instance of the progra when needed. Any advice on what I can do to correct my approach is appreciated

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

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

发布评论

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

评论(3

如此安好 2024-10-02 09:06:10

使用 -o 选项控制输出哪些数据以及按什么顺序输出。

例如

$ ps -o pid,time,comm
  PID     TIME COMMAND
 3029 00:00:01 zsh
22046 00:00:00 ps

$ ps -o pid,time,comm --no-headers
 3029 00:00:01 zsh
22046 00:00:00 ps

这将使它更容易解析。我建议使用 python 解析输出 (pid, time, cmd) = result.split(2)

或者使用 pgrep 命令仅列出与给定条件匹配的进程。

Use the -o option to control what data is output and in what order.

e.g.

$ ps -o pid,time,comm
  PID     TIME COMMAND
 3029 00:00:01 zsh
22046 00:00:00 ps

or

$ ps -o pid,time,comm --no-headers
 3029 00:00:01 zsh
22046 00:00:00 ps

This will make it easier to parse. I would suggest parsing the output with python using (pid, time, cmd) = result.split(2)

Alternatively use the pgrep command to only list processes that match given criteria.

霞映澄塘 2024-10-02 09:06:10

怎么样使用

ps ux | awk '/$PROCESS_NAME/ && !/awk/ {print $2, $10}'

它应该给你 PID 和 TIME
(改编自此处

编辑:
-ef 的版本

ps -ef | awk '/$PROCESS_NAME/ && !/awk/ {print $2, $7}'

What about using

ps ux | awk '/$PROCESS_NAME/ && !/awk/ {print $2, $10}'

It should give you PID and TIME
(adapted from here)

EDIT:
Version with -ef

ps -ef | awk '/$PROCESS_NAME/ && !/awk/ {print $2, $7}'
涫野音 2024-10-02 09:06:10

如果您想要一致地表示 CPU 时间,请读取 /proc//stat 并读取 utimestime 字段。

阅读 man 5 proc 以获得该文件的完整布局,但简单的是您需要第 14 个和第 15 个值,它们以“jiffies”表示(通常为每秒 100 个),例如

% ps -e 2398
2398 ?        00:04:29 ypbind

00:04:29 是 269 秒

% awk '{ print ($14 + $15) / 100 }' < /proc/2398/stat
269.26

,或者在不生成子进程的 Bash 脚本中:

#!/bin/sh
read -a procstat /proc/pid/stat
let cpu=(procstat[13] + procstat[14]) / 100

If you want consistent representation of the CPU time, read /proc/<pid>/stat and read the utime and stime fields.

Read man 5 proc to get the full layout of that file, but the easy bit is that you need the 14th and 15th values, and they're represented in "jiffies" (typically 100 per second), e.g.

% ps -e 2398
2398 ?        00:04:29 ypbind

00:04:29 is 269 seconds

% awk '{ print ($14 + $15) / 100 }' < /proc/2398/stat
269.26

or, in a Bash script without spawning a sub-process:

#!/bin/sh
read -a procstat /proc/pid/stat
let cpu=(procstat[13] + procstat[14]) / 100
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文