脚本来检查正常运行时间,awk其输出并将其输出与gt 90天进行比较,否则少于90天

时间:2019-06-27 11:12:24

标签: shell

我想开发一个脚本来比较服务器超过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"

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

1 个答案:

答案 0 :(得分:1)

没有理由使用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
相关问题