首页 > 解决方案 > 检查正常运行时间的脚本,awk 其输出并将其输出与 gt 90 天进行比较,否则少于 90 天

问题描述

我想开发一个脚本来比较服务器的正常运行时间超过 90 天。

我已经制作了一个脚本,需要意见以使其更好,并询问这是否可以正常工作或需要一些更正。

#!/bin/sh
output=`uptime | grep -ohe 'up .*' | sed 's/,//g' | awk '{ print $2" "$3 }'`
echo $output
if [ $output -gt "90 days"]
echo "Uptime is greater then 90 days"
else 
echo "Uptime is less then 90 days"

我想将此脚本作为错误修复包运行,以检查 Linux 使用服务器的输出,这些服务器的正常运行时间超过 90 天,并且需要帮助将输出存储在 /tmp 中的文件中。

标签: shell

解决方案


没有理由使用 grep、sed 和 awk。这是一个仅使用 awk 的 Linux,从/proc/uptime. man proc

/proc/uptime
       This  file  contains  two  numbers:  the uptime of the system (seconds), 
       and the amount of time spent in idle process (seconds).

让我们来看看:

$ uptime
 14:36:40 up 21 days, 20:04, 12 users,  load average: 0.78, 0.85, 0.88
$ cat /proc/uptime
1886682.73 1652242.10

一个 awk 脚本:

$ awk '{
    if($1>90*24*3600)
        print "Uptime is greater than 90 days"
    else 
        print "Uptime is less than or equal to 90 days"
}' /proc/uptime 

我的系统的输出:

Uptime is less than or equal to 90 days

推荐阅读