데비안에서 '가동 시간 기록' 로그를 유지하는 방법은 무엇입니까?

데비안에서 '가동 시간 기록' 로그를 유지하는 방법은 무엇입니까?

현재 다음을 사용하여 서버에서 가동 시간을 HTML 페이지로 출력합니다.

TIME=$(uptime -p) echo ""${TIME}"!" >> /var/www/html/index.new

다음과 같은 출력이 생성됩니다.

1일 1시간 2분!

또한 (호기심을 위해) 내 시스템의 기록 가동 시간을 표시하고 싶습니다. 하지만 이를 기록하고 (uptime -p) [day, hr, min]에 다시 표시하는 가장 좋은 방법은 확실하지 않습니다. ] 형식입니다.

이를 수행할 수 있는 기존 도구가 있습니까? 아니면 파일에 가동 시간을 기록하고 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-cgiuptimed웹 페이지에 저장된 정보를 표시합니다 . 기본적으로는 다음에서 활성화됩니다.http://localhost/cgi-bin/uprecords.cgi.

관련 정보