【问题标题】:Shell Script To Notify When Server Uptime Has Reached Threshold当服务器正常运行时间达到阈值时通知的 Shell 脚本
【发布时间】:2020-04-18 16:33:07
【问题描述】:

我正在寻找清理以下 shell 脚本,该脚本读取 Linux 服务器的正常运行时间,并在达到阈值时向特定用户发送邮件,以便我们知道何时通过每日 cron 作业重新启动。下面将读取正常运行时间并将结果输出到文件,然后通过电子邮件发送。我还想在服务器的主机名中添加报告或电子邮件标题。 shell脚本相当新,所以任何/所有提示都值得赞赏。谢谢!

#!/bin/bash
timeup () { uptime | awk  '{print $3}'; }
UPTIME=100
if [ $(timeup) -ge $UPTIME ]; then

    #output uptime to report
    uptime | awk '{print $3,$4}' | sed 's/,//' >> /opt/scripts/report.out 

    #mail report 
    cat /opt/scripts/report.out | mail -s "Server Needs To Be Rebooted" "your@emailaddress.com"

    #remove old file
    rm -f /opt/scripts/report.out   
fi

【问题讨论】:

    标签: linux shell uptime


    【解决方案1】:

    timeup () { uptime | awk '{print $3}'; } 结果将类似于:“45:45,”。这有两个问题:

    • 返回值将是字符串而不是整数,因此 if 行将发送错误消息
    • "," 在字符串的 and 处签名

    这更好(但不是很好:)): timeup () { uptime | awk '{print $3}' | awk -F\: '{print $1}';}

    第二个 awk 命令除以小时和分钟,并且只打印小时。

    我更喜欢 sendEmail 脚本而不是邮件命令:http://manpages.ubuntu.com/manpages/xenial/man1/sendEmail.1.html

    我会在主题中输入主机名:"Server Needs To Be Rebooted: $(hostname -f)"

    hostname -f 命令打印出 FQDN

    您可以合并 uptime ...ma​​il 命令行并删除 rm 命令:

    uptime | awk '{print $3,$4}' | sed 's/,//' | mail -s "Server Needs To Be Rebooted" "your@emailaddress.com"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-27
      • 2015-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多