【问题标题】:Execute script after ntpd syncntpd 同步后执行脚本
【发布时间】:2015-01-22 01:36:21
【问题描述】:

我有 raspbian 的树莓派。我想在时间与 ntpd 同步后立即执行一个脚本,我的脚本需要正确的日期时间。我该怎么做?

【问题讨论】:

    标签: linux raspberry-pi ntpd


    【解决方案1】:

    正确的做法是使用ntp-waitntp-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.
    

    【讨论】:

    • 如果 ntp-wait 有类似 -n 0 的选项(永远等待),我希望它更好读取代码会与大数字混淆并计算 secs*tries 以确保它没有任何意义......我会抱怨定制西装不合身:)
    【解决方案2】:

    假设您有一个有权调用 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”才能使其正常工作。

    【讨论】:

    • 我不懂$?部分 $ 是什么?
    • 抱歉,很难搜索 $?在谷歌,但我终于想通了,$?是命令退出状态
    • 我刚看到你的问题,抱歉。我很高兴听到你想通了。对,美元?保存 ntpdate 的退出状态,仅在有效设置日期时返回零。
    猜你喜欢
    • 2010-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多