shell脚本:如何将进程运行时间与阈值进行比较?

发布于 2024-12-20 19:16:02 字数 239 浏览 0 评论 0原文

Bash 脚本应该检查某个进程的运行时间是否超过一定的分钟数,如果超过则终止它。

我可以通过类似 That Gives 9:47.31 的方式来获取运行时间

ps -aux | grep ProgramName | grep -v grep | awk '{print $10}'

例如, 。但我应该在哪里进一步检查该值是否大于(例如 10 分钟)阈值?

Bash script should check if a certain process is running more than a certain number of minutes, and kill it if does.

I can get the running time by something like

ps -aux | grep ProgramName | grep -v grep | awk '{print $10}'

That gives 9:47.31 for instance. But where do I go further and check if that is greater than, say 10 minutes threshold?

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

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

发布评论

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

评论(4

因为看清所以看轻 2024-12-27 19:16:02

以下是您的用例所需的 awk 1 衬垫:

ps -o etime -C ProgramName | awk -v MAX=600 '{split($0, a, ":"); if (length(a)==2) sec=a[1]*60+a[2]; else if (length(a)==3) sec=a[1]*3600+a[2]*60+a[3]; if (sec>MAX) print "Elapsed"; else print "Not Elapsed"}'

另请注意,ps -o etime -C ProgramName 为您提供自 ProgramName 运行以来的时间,因此您无需使用您的这次获取的命令过于复杂。

重要提示:另请记住,对于运行超过一天的进程,您将获得 ps 命令的输出,如 1-21:48 所示: 48..我的 awk 命令中没有涵盖这种情况,但您可以使用与上面所示相同的 awk split 命令。

更新:根据下面的评论,请将此版本用于 FreeBSD 或任何其他风格的 Unix(例如:Mac),其中 -C ProgramName 选项不可用。

ps -o etime=,command= | awk -v MAX=600 '/ProgramName/ && !/awk/ {split($1, a, ":"); if (length(a)==2) sec=a[1]*60+a[2]; else if (length(a)==3) sec=a[1]*3600+a[2]*60+a[3]; if (sec>MAX) print "Elapsed"; else print "Not Elapsed"}'

Here is the awk 1 liner you'll need for your use case:

ps -o etime -C ProgramName | awk -v MAX=600 '{split($0, a, ":"); if (length(a)==2) sec=a[1]*60+a[2]; else if (length(a)==3) sec=a[1]*3600+a[2]*60+a[3]; if (sec>MAX) print "Elapsed"; else print "Not Elapsed"}'

Also note that ps -o etime -C ProgramName gives you the time since ProgramName has been running so you don't need to use your overly complicated command to get this time.

IMPORTANT: Also remember that for the processes that have been running for more than a day you will get output of ps command as something like 1-21:48:48. I don't have this case covered in my awk command but you can use the same awk's split command as I have shown above.

UPDATE: As per the comment below, use this version for FreeBSD or any other flavor of Unix (eg: Mac) where -C ProgramName option is not available.

ps -o etime=,command= | awk -v MAX=600 '/ProgramName/ && !/awk/ {split($1, a, ":"); if (length(a)==2) sec=a[1]*60+a[2]; else if (length(a)==3) sec=a[1]*3600+a[2]*60+a[3]; if (sec>MAX) print "Elapsed"; else print "Not Elapsed"}'
吻风 2024-12-27 19:16:02

这是一种可能的方法:

for time in `ps auxwww  | awk '{print $10}'`; 
do  
SEC=`echo $time | cut -d":" -f2`; 
MIN=`echo $time | cut -d":" -f1`; 
TOTALTIMEINSEC=`echo $SEC+$MIN*60 | bc`; 
echo "the time in sec is:" $TOTALTIMEINSEC; done

顺便说一句,您不需要 gerp -v grep,您可以这样做:

grep [P]rogramName

也就是说,我很想看到其他解决方案,因为我觉得我正在回收这种方法......

Here is one possible way:

for time in `ps auxwww  | awk '{print $10}'`; 
do  
SEC=`echo $time | cut -d":" -f2`; 
MIN=`echo $time | cut -d":" -f1`; 
TOTALTIMEINSEC=`echo $SEC+$MIN*60 | bc`; 
echo "the time in sec is:" $TOTALTIMEINSEC; done

BTW, you don't need to gerp -v grep, you can do:

grep [P]rogramName

That said, I'd love to see other solution, because I feel I'm recycling this methods...

菊凝晚露 2024-12-27 19:16:02

首先,您可以使用以下命令来避免不必要的 grep -v grep 和 awk dance:

$ ps -o time `pidof ProgramName`

在我的 Linux 机器上,这似乎以 HH:MM:SS 格式给出时间。

考虑到 pidof ProgName 可能会给出多个值,您可以使用 tail -n +2|head -1 或类似的东西来处理它。

现在要获取持续时间,您可以将时间转换为秒:

$ seconds=$(printf "%d * 3600 + %d * 60 + %d\n" $(ps -o time $(pidof ProgramName)|tail -n +2|head -1|sed -e 's/:/ /g')|bc)

请注意,ps -o time 给出的时间也可能采用以下格式:D-HH:MM:SS > 其中 D 是天数。

First, you can avoid the unnecessary grep -v grep and awk dance with the following instead:

$ ps -o time `pidof ProgramName`

On my linux machine this seems to give the time in the format HH:MM:SS.

Taking into consideration that pidof ProgName might give more than one value you might handle that with tail -n +2|head -1 or something like that.

Now to get the duration you can convert the time into seconds:

$ seconds=$(printf "%d * 3600 + %d * 60 + %d\n" $(ps -o time $(pidof ProgramName)|tail -n +2|head -1|sed -e 's/:/ /g')|bc)

Note that the time given by ps -o time might be in this format too: D-HH:MM:SS where D is the number of days.

触ぅ动初心 2024-12-27 19:16:02

这适用于程序运行时间少于一天的情况

THRESH=360
ps auxwww | grep [P]rocessname | awk '{print $10}' | sed -e 's/:/ /; s/\.[0-9]*$//' | while read m s; do 
  let total=${m}*60+${s}
  if [ $total -gt $THRESH ]; then 
    echo "${total} seconds total is over threshold of ${THRESH} seconds"
  fi
done

如果您想要更高的阈值,您将需要在提取进程时间方面添加更多逻辑,但那时我会将内容放入 perl/ruby 中脚本并通过 `ps auxwww` 获取信息

This will work for cases where your program has run less than a day

THRESH=360
ps auxwww | grep [P]rocessname | awk '{print $10}' | sed -e 's/:/ /; s/\.[0-9]*$//' | while read m s; do 
  let total=${m}*60+${s}
  if [ $total -gt $THRESH ]; then 
    echo "${total} seconds total is over threshold of ${THRESH} seconds"
  fi
done

If you want higher thresholds, you'll want to put some more logic around the extraction of process time, but at that point I'd put things into a perl/ruby script and get the information via `ps auxwww`

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文