使用“ps”查找某个时间范围内的进程
我如何找到最近 5 小时内开始处理的内容? ps
可以做到吗?
我必须使用 ps -ef | grep
How can I find started processed within last 5 hours? Can ps
do that?
I have to use ps -ef | grep <username>
which shows all process for . Then I have to manually look at STIME columns
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ps -eo etime,pid
将以[[DD-]hh:]mm:ss
格式列出所有 PID 以及自进程创建以来经过的时间。这可能会派上用场,因为您可以搜索小于 5:00:00 的时间量,而不是执行更复杂的日期比较。ps -eo etime,pid
will list all PIDs together with the elapsed time since the process creation in the format[[DD-]hh:]mm:ss
. This may come in handy as you can then search for time amounts less than 5:00:00 instead of performing trickier date comparisons.也许这对你有帮助:
功劳在于: 你如何杀死所有超过一定年龄的 Linux 进程?
maybe this helps you:
credit goes to: How do you kill all Linux processes that are older than a certain age?
stat -t /proc/; | awk '{print $14}'
将为您提供自纪元以来进程的启动时间(以秒为单位)。与当前时间 (
date +%s
) 进行比较,以查找进程的生存期(以秒为单位)。stat -t /proc/<pid> | awk '{print $14}'
will give you the start time of a process in seconds since the epoch. Compare with current time (
date +%s
) to find the age of a process in seconds.只是为了搞笑,这是我必须在一些古老的系统(aix 和Solaris)上做的事情,到目前为止似乎“还可以”......(但当将来发生变化时可能会失败。另外,我认为它在进程持续超过 999 天的机器上已经失败了......当我找到一个时我会修复这个)
在那些古老的系统上:
下面的小东西照顾这些情况(以及更多)...但真的很丑 ^^
编辑:新版本(有点理智,多才多艺,但仍然表明你需要处理 ps 输出很多以获得一些可用的东西...这要归功于它有一种奇怪的方式来改变它显示的信息数量...)
旧版本:
复杂的 sed (如上一个) )尝试保持对齐(以及输出比 ps 更对齐)。 (如果可以的话,我会使用 awk,但是由于更改 $1、$2、... 或 $0 上的任何内容都会重新计算整行,所以我无法轻松保持对齐。也许我应该只做简单的事情并通过更简单的 printf 来重新对齐?......我稍后会这样做!(但我担心长参数行可能会把事情搞砸......而且我不知道如何告诉 printf“只需格式化前几个参数,然后将其他所有内容按原样放置在最后 %s")
添加回常规 ps '时间' 将是一项相当大的工作,因为无论该过程的日期是多于还是少于 24 小时,它都有 1 或 2 列,并且间距将再次变得完全错误,最后一个 sed 会失败,等等。
享受 ^^ [它仍然是一个正在进行的工作...到目前为止有效,但我无法在其他系统上尝试它(linux?其他版本的 aix 和 aix)索拉里斯, ETC)]
Just for the laughs, here is what I had to do on some ancient systems (both aix and solaris), and it seems just about "ok" up to now... (but may fail when something changes in the future. Also, I think it already fails on machines where processes last for more than 999 days... I'll fix this when I find one)
On those ancient systems:
The little thing below takes care of those cases (and more) ... but is really ugly ^^
Edit: New version (a bit saner, and versatile, but still show you need to process ps output a lot to get something usable... thanks to it having a weird way of changing the number of information it displays...)
Old version:
The convoluted sed (like the last one) try to keep the alignement (and the output is more aligned than what ps does). (I would use awk if I could, but as changing anything on $1, $2, ... or $0 recomputes the whole line, I can't easily keep the alignement. maybe I should just do simple things and process it all via a simpler printf to realign?.... I will do that sometime later! (but I fear that long args lines may mess things up... And I don't know how to tell printf "just format the first few args, and put everything else as-is in the last %s")
Adding back-in the regular ps 'time' would be quite some work, as it have 1 or 2 columns whether the process is dated of more or less than 24h, and the spacing would become all wrong again, and the last sed would fail, etc.
Enjoy ^^ [and it is still a work-in-progress... works so far, but I couldn't try it on other systems (linux? other versions of aix & solaris, etc)]