Linux / Bash 使用 PS -f 获取与 PS -f 不同格式的特定 PID 返回,还有关于使用 Grep 解析此问题的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3785768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linux / Bash using PS -f for specific PID returns in different format than PS -f, also queston about using Grep to parse this
提问by Rick
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"
对于我正在创建的 python 脚本,我需要首先获取进程的 PID(基于其名称),然后从该进程中获取,使用其 PID、其持续时间,从打印输出下面,将是“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:
我正在使用它来获取进程名称的 PID:
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)
所以,这很好用,我假设剪切参数(-c10-15)会普遍工作,因为 PID 的位置不应该改变(我只是从我发现的片段中得到这个)
However, when I try to do something similar to get just the TIME value, such as this it returns it differently
但是,当我尝试执行类似的操作以仅获取 TIME 值时,例如它会以不同的方式返回它
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.
因此,当我尝试剪切时,如下所示,我认为它不会正常工作,因为我不确定此返回的间距是否一致,并且它显示的时间值与以前不同(不是原始的“00:00 :00" 样式的打印输出。
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
我在我的 python 脚本中使用它来记录特定进程类型的 PID(按名称),以便我以后可以在需要时关闭该程序实例。任何关于我可以做些什么来纠正我的方法的建议表示赞赏
采纳答案by Dave Kirby
Use the -o option to control what data is output and in what order.
使用 -o 选项来控制输出什么数据以及以什么顺序输出。
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)
这将使解析更容易。我建议使用 python 解析输出(pid, time, cmd) = result.split(2)
Alternatively use the pgrep command to only list processes that match given criteria.
或者,使用 pgrep 命令仅列出与给定条件匹配的进程。
回答by Alnitak
If you want consistent representation of the CPU time, read /proc/<pid>/statand read the utimeand stimefields.
如果您想要 CPU 时间的一致表示,请阅读/proc/<pid>/stat和阅读utime和stime字段。
Read man 5 procto 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.
阅读man 5 proc以获取该文件的完整布局,但简单的一点是您需要第 14 个和第 15 个值,它们以“jiffies”(通常每秒 100 个)表示,例如
% ps -e 2398
2398 ? 00:04:29 ypbind
00:04:29 is 269 seconds
00:04:29 是 269 秒
% awk '{ print ( + ) / 100 }' < /proc/2398/stat
269.26
or, in a Bash script without spawning a sub-process:
或者,在不生成子进程的 Bash 脚本中:
#!/bin/sh
read -a procstat /proc/pid/stat
let cpu=(procstat[13] + procstat[14]) / 100

