
現在、私は以下のコマンドを使用して、サーバーの稼働時間を 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。