Linux / Bash 使用 PS -f 获取特定 PID 以与 PS -f 不同的格式返回,还有关于使用 Grep 解析此问题的问题
对于我正在创建的 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
来记录特定进程类型(按名称)的 PID,以便我以后可以在需要时关闭该程序的实例。赞赏
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 -o 选项控制输出哪些数据以及按什么顺序输出。
例如
或
这将使它更容易解析。我建议使用 python 解析输出
(pid, time, cmd) = result.split(2)
或者使用 pgrep 命令仅列出与给定条件匹配的进程。
Use the -o option to control what data is output and in what order.
e.g.
or
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.
怎么样使用
它应该给你 PID 和 TIME
(改编自此处)
编辑:
带
-ef
的版本What about using
It should give you PID and TIME
(adapted from here)
EDIT:
Version with
-ef
如果您想要一致地表示 CPU 时间,请读取
/proc//stat
并读取utime
和stime
字段。阅读 man 5 proc 以获得该文件的完整布局,但简单的是您需要第 14 个和第 15 个值,它们以“jiffies”表示(通常为每秒 100 个),例如
00:04:29 是 269 秒
,或者在不生成子进程的 Bash 脚本中:
If you want consistent representation of the CPU time, read
/proc/<pid>/stat
and read theutime
andstime
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.00:04:29 is 269 seconds
or, in a Bash script without spawning a sub-process: