【问题标题】:Notification for process that has been active for more than X time激活超过 X 时间的进程的通知
【发布时间】:2018-10-23 03:54:26
【问题描述】:

我对 linux 脚本很陌生,但我正在尝试创建此脚本以添加到 crontab 中,以检查用户进程是否已运行超过 x 时间。

#! /bin/sh
#This feeds the list of process over 1 hour and strips the colons and sees 
#if it is over 12 hours. If so, then mails it to me.
if $(( (ps exo time,pid | grep -v "00:00:" | awk '{print $1}' | sed s/://g) >= 001200 )); then
    mailx -s "$(hostname) Long Process Detection" me@example.com
fi

截至目前,它正在产生一个错误,它缺少一个')',但我已经看过很多次了,我看不到任何未闭合的括号。对此的任何帮助将不胜感激。错误显示

"missing ')' (error token is "exo time,pid |grep -v "00:00:" | awk '{print }' | sed s/://g) >= 001200 ")"

上面的问题是通过在左括号内移动 $ 来解决的,但是它在条件计算中反馈了一个错误。 (即我的示例中的 000106 >= 000010)。

【问题讨论】:

  • 很难说脚本或复制粘贴是否有错误,但 if 子句缺少“)”。 3开,2关。
  • 啊,那是我的版本中的错误。来自记事本与服务器。我在收到错误的服务器上关闭了 3 个。
  • 邮件的内容应该是什么?如果存在运行时间超过 12 小时的任何进程,您是否希望 if 条件为真?
  • 最好是用户进程,而不是root。但在我修复它之前,我只是想让它解决任何过程。您之前的解决方案修复了丢失的 ),但在计算数值时出现问题。 (即 000106 >= 000010 是我的样本)。邮件正文将只包含进程用户 ID、进程 ID 和时间。

标签: shell scripting


【解决方案1】:

实际上,不确定您的代码,但这应该是您想要的,我希望:

#!/bin/sh
#This feeds the list of process over 1 hour and strips the colons and sees
#if it is over 12 hours. If so, then mails it to me.
for i in $(ps exo time,pid | grep -v "00:00:" | grep -v "TIME" | awk '{print $1}' | sed 's/://g');do
    if [ $i -ge 001200 ]; then
            #echo $i
            mailx -s "$(hostname) Long Process Detection" me@example.com
    fi
done

只需使用评论来发送邮件或测试找到的值。

【讨论】:

  • 鉴于awk 可以做任何grep 和sed 可以做的事情,grep ... | grep ... | awk ... | sed ... 的序列是相当丑陋的;-)
猜你喜欢
  • 2015-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多