shell - 检查正常运行时间的脚本,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 中的文件中。
解决方案
没有理由使用 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
推荐阅读
- python - Python中的语法错误
- regex - 使用正则表达式批量替换
- hibernate - Hibernate 5.4.0 缺少标记化选项
- tensorflow - 以字母数字代码为特征的 TensorFlow 分类
- ios - 无法在 iOS 13.3.1 中关闭 Google Admob 广告:控制台日志:[Process] kill() 返回意外错误 1
- c - 数据类型和变量之间的关键字
- python - Python中的诅咒模块Eroor
- python - Python 中的单独 print() 结果
- reactjs - 如何防止 useEffect 中的函数被多次触发?
- python - matplotlib 动画,用于在单个图形中同步移动多个形状