运行超过x分钟的进程的Grep计数 [英] Grep count of processes running more than x minutes
问题描述
我的目标是在bash中使用grep记录比"x"分钟还早的进程.
My goal is to grep for a count of processes older than 'x' minutes in bash.
到目前为止,我可以对所有进程执行时间进行grep设置:
So far, I can grep for all the process execution times:
ps -eo etime,cmd | grep mysuperspecialprocess.sh | grep -Ev 'grep' | awk '{print $1}'
我用管道输送wc -l
在末尾计数.
I pipe a wc -l
to get a count at the end.
如何对结果进行grep或循环遍历以将其限制为超过一定分钟数的进程?
How can I grep or loop through the result to restrict it to processes older than a certain number of minutes?
推荐答案
尝试通过此测试版本.
Give this tested version a try.
它将搜索指定的运行进程,并计算所有关联事件的经过时间大于指定秒数的事件.
It searches a specified running process, and count all occurences which has an associated elapsed time greater than a specified number of seconds.
首先设置参数的值并运行它.
Set parameters' values first and run it.
$ cat ./script.sh
#!/bin/bash --
process_name=mysuperspecialprocess.sh
elapsed_time_seconds=600
ps -eo etimes,cmd | fgrep "${process_name}" | fgrep -v fgrep | ( count=0 ; while read etimes cmd ; do \
if [ $etimes -gt $elapsed_time_seconds ] ;\
then \
count=$((count+1)) ;\
fi ;\
done ; echo $count )
倍次数指示 ps 显示秒数.
这篇关于运行超过x分钟的进程的Grep计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!