2015-01-22 2 views
3

나는 라스베가스 파이가 있습니다. 시간이 ntpd와 동기화되면 바로 스크립트를 실행하고 싶습니다. 스크립트에 올바른 datetime이 필요합니다. 내가 어떻게 할 수 있니?ntpd 동기화 후 스크립트 실행

답변

1

ntpdate를 호출 할 수있는 권한이있는 사용자 (시스템의 시간을 조정할 수있는 사용자)가 있다고 가정 할 때 다음 스크립트를 사용할 수 있습니다. 예를 들어 ntp 서버 "0.ca를 사용하고 있습니다. pool.ntp.org "

#!/bin/bash 

NEEDS_SYNC=1 
while [ "$NEEDS_SYNC" -ne "0" ]; do 
    ntpdate -t 4  0.ca.pool.ntp.org 
    NEEDS_SYNC=$? # If this variable is set ot 0, time sync worked 
    sleep 2 
done 

# RUN THE SCRIPT THT NEEDS ntp SYNC'D TIME HERE 

이 기능을 사용하려면 'ntpdate'패키지를 설치해야 할 수도 있습니다.

+0

나는 $을 이해하지 못합니까? $는 무엇입니까? – davidovv

+0

죄송합니다. $을 (를) 검색하기가 어려웠습니까? 구글에서,하지만 마침내 알아 냈어, $? 명령 종료 상태입니다. – davidovv

+0

질문을 보았습니다. 죄송합니다. 나는 당신이 그것을 알아 낸 것을 기뻐합니다. 맞았 어, $? ntpdate의 종료 상태를 유지합니다. ntpdate는 날짜가 효과적으로 설정 될 때만 0을 반환합니다. –

3

올바른 방법은 ntp-wait을 사용하는 것입니다. ntp-wait은 이러한 유형의 상황에 맞게 조정되었습니다. 다음은 매뉴얼 페이지입니다 :

ntp-wait(1)      User Commands     ntp-wait(1) 

NAME 
     ntp-wait - Wait for ntpd to stabilize the system clock 

SYNOPSIS 
     ntp-wait [-flag [value]]... [--opt-name [[=| ]value]]... 

     All arguments must be options. 

DESCRIPTION 
     will send at most num-tries queries to sleeping for secs-between- 
     tries after each status return that says has not yet produced a syn‐ 
     chronized and stable system clock. 

     will do this quietly, unless the v flag is provided. 

OPTIONS 
     -n num-tries, --=num-tries 
       Number of times to check ntpd. This option takes an integer 
       number as its argument. The default num-tries for this option 
       is: 
        100 

       The maximum number of times we will check ntpd to see if it 
       has been able to synchronize and stabilize the system clock. 

     -s secs-between-tries, --=secs-between-tries 
       How long to sleep between tries. This option takes an integer 
       number as its argument. The default secs-between-tries for 
       this option is: 
        6 

       We will sleep for @file{secs-between-tries} after each query 
       of ntpd that returns "the time is not yet stable". 

     -v, -- Be verbose. 

       By default, ntp-wait is silent. With this option, ntp-wait 
       will provide status information. 

     -?, --help 
       Display usage information and exit. 

     -!, --more-help 
       Pass the extended usage information through a pager. 

     - [{v|c|n}], --version[={v|c|n}] 
       Output version of program and exit. The default mode is `v', 
       a simple version. The `c' mode will print copyright informa‐ 
       tion and `n' will print the full copyright notice. 


EXIT STATUS 
     One of the following exit values will be returned: 

     0  Successful program execution. 

     1  The operation failed or the command syntax was not valid. 
+0

만약 ntp-wait가 -n 0 (영원히 기다림)과 같은 옵션을 가지고 있다면 ntp-wait를 더 좋아할 것입니다. 현재 옵션으로는 (내 인생의 추정 나머지와 같이) 매우 큰 숫자를 써야하고 코드를 읽는 다른 누군가는 큰 숫자와 초를 계산 * 아무 의미가 없다는 것을 인정하려고 ... 나는 양복이 맞지 않는다는 것을 재단사에게 불평 할 것이다. :) – davidovv