
目前,我的伺服器使用以下命令將正常運行時間輸出到 HTML 頁面:
TIME=$(uptime -p) echo ""${TIME}"!" >> /var/www/html/index.new
其生成的輸出為:
增加1天1小時2分鐘!
我還希望(出於好奇)能夠顯示我的系統的記錄正常運行時間,儘管不確定記錄該記錄並將其顯示回(正常運行時間-p)[天,小時,分鐘]的最佳方式] 格式。
是否有現有的工具可以做到這一點?或者我是否需要將正常運行時間記錄到文件中並使用 grep 或類似的東西提取最高值?
答案1
安裝“uptimed”後,我在腳本中添加了以下幾行:
uprecords | head -n 4 | tail -n 1 | awk '{print "Current uptime record is " $3,$4$5,$6" (hr:mis:sec)"}'
目前正常運作時間記錄為 1 天 02:05:34(小時:分鐘:秒)
這應該非常適合我的需求。
答案2
我建議使用該-s
標誌來取得系統啟動時間的可解析時間。然後再用date
和減法。
繼續循環執行此操作並將其與記錄進行比較。當然,錄製時間需要保存到文件中。
(記錄檔需要初始化。 echo 0 > record
)
只要這個腳本繼續運行,任何東西都可以簡單地讀取記錄檔案來找出當前的記錄是什麼。
#!/bin/sh
format_time ()
{
t=$1
days=$(($t / 86400))
t=$(($t - 86400*$days))
hours=$(($t / 3600))
t=$(($t - 3600*$hours))
minutes=$(($t / 60))
t=$(($t - 60*$minutes))
echo "$days days $hours hours $minutes minutes $t seconds"
}
main ()
{
# Old record
record="$(cat record)"
# When the system started.
boot="$(date -d "$(uptime -s)" '+%s')"
while true; do
# You could also calculate the uptime before the loop and just
# increment it, but that will rely on the script running 24/7.
now="$(date '+%s')"
current_uptime="$(($now - $boot))"
# Set and save the record when it gets beaten
if [ "$current_uptime" -gt "$record" ]; then
record="$current_uptime"
echo "$record" > record
fi
# Status should be printed _after_ potentially having set
# a new record:
clear
echo "Current uptime: $(format_time "$current_uptime")"
echo "Record uptime: $(format_time "$record")"
# Don't waste system resources for silly things.
sleep 1
done
}
main
答案3
既然您已經在使用uptimed
,也可以安裝uprecords-cgi
uptimed
它呈現網頁中儲存的資訊;預設情況下它將被啟用http://localhost/cgi-bin/uprecords.cgi。