linux - Grep 运行超过 x 分钟的进程计数

标签 linux bash shell awk

我的目标是在 bash 中 grep 查找早于“x”分钟的进程计数。

到目前为止,我可以 grep 获取所有进程执行时间:

ps -eo etime,cmd | grep mysuperspecialprocess.sh | grep mysuperspecialprocess.sh | grep -Ev 'grep' | grep -Ev 'grep' | grep -Ev 'grep' awk '{print $1}'

我通过管道传输 wc -l 以在最后获取计数。

如何 grep 或循环遍历结果以将其限制为早于特定分钟数的进程?

最佳答案

尝试一下这个经过测试的版本。

它搜索指定的正在运行的进程,并对所有相关运行时间大于指定秒数的事件进行计数。

先设置参数值并运行。

$ 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 )

etimes 指示 ps 显示秒。

关于linux - Grep 运行超过 x 分钟的进程计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35873018/

相关文章:

mysql - phpmyadmin (linux) 删除 mysql SQL 查询窗口中的反引号

可以接受无限参数的 Linux Bash 脚本

linux - 检查目录中是否存在某种文件类型/扩展名

linux - 我的计算引擎 VM 没有从我的存储库中获取更改

shell - 如何在 shell 中取消设置 vim 模式

linux - 不使用 "crontab -e"命令编辑crontab文件有什么风险?

mysql - 编辑 Bash 脚本以显示输出

linux - 如何将系统资源的百分比分配给Docker?

linux - 如何使用带有 bash 的 sudo tee 在文件中写入 $something?

PHP-Fpm.d 与 nginx ,如何在 shell exec 中运行 sudo