Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无限制地重复bash命令,但仅在上一个bash命令完成时重复_Bash_Ntp - Fatal编程技术网

无限制地重复bash命令,但仅在上一个bash命令完成时重复

无限制地重复bash命令,但仅在上一个bash命令完成时重复,bash,ntp,Bash,Ntp,在我们用于构建的macOs虚拟机上,时间有时会无缘无故地跳跃。作为一种解决方法,我创建了名为test.sh的脚本,该脚本始终更正时间: #!/bin/bash -e while true; do sudo ntpdate -u de.pool.ntp.org >> ntpdate.txt; sleep 30; done 在构建开始时,这将在后台开始: ./test.sh & 当构建完成时,我会杀死它: kill $(ps aux | grep test.sh |

在我们用于构建的macOs虚拟机上,时间有时会无缘无故地跳跃。作为一种解决方法,我创建了名为
test.sh
的脚本,该脚本始终更正时间:

#!/bin/bash -e

while true; do
    sudo ntpdate -u de.pool.ntp.org >> ntpdate.txt; sleep 30;
done
在构建开始时,这将在后台开始:

./test.sh &
当构建完成时,我会杀死它:

kill $(ps aux | grep test.sh | grep -v grep | awk '{print $2}')

有时,调用更新时间需要超过30秒。然后有两个打开的ntp池调用,我得到了一个速率限制响应。因此,我想限制对ntp的调用一次只能调用一个。如何在while-true循环中实现这一点?

在Bash中实现互斥的一种简单方法是使用“lockfile”。检查文件是否存在,如果存在,则不执行NTP查询。如果文件不存在,则创建它。在崩溃情况下,一个有用的增强功能是检查文件时间是否超过几分钟,在这种情况下可以将其删除。

在Bash中实现互斥的一个简单方法是使用“锁定文件”。检查文件是否存在,如果存在,则不执行NTP查询。如果文件不存在,则创建它。在崩溃情况下,一个有用的增强功能是检查文件时间是否超过几分钟,在这种情况下可以将其删除。

能否尝试查看以下内容是否适用于您的情况

#!/bin/bash

while true; do
  pid=0
  sudo ntpdate -u de.pool.ntp.org >> ntpdate.txt & pid=(${!})
  wait $pid
done

你能试着看看下面的方法是否适用于你的案例吗

#!/bin/bash

while true; do
  pid=0
  sudo ntpdate -u de.pool.ntp.org >> ntpdate.txt & pid=(${!})
  wait $pid
done

谢谢你的建议。正如Charles Duffy提到的,我的代码只有在前一个命令完成后才会继续。我用以下方法对其进行了测试:

#!/bin/bash -e

while true; do
    echo "start ntpdate"
    sudo ntpdate -u de.pool.ntp.org >> ntpdate.txt; 
    echo "going to sleep"
    sleep 30;
done
所以很明显,睡眠计时器太低,无法运行到限速响应。也许我得提高睡眠时间

正如chepner所建议的,我创建了一个LaunchAgent来处理此问题:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>workaroundAgainstJumpingTime</string>
    <key>ProgramArguments</key>
    <array>
        <string>sudo</string>
        <string>ntpdate</string>
        <string>-u</string>
        <string>de.pool.ntp.org</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/tmp/workaroundAgainstJumpingTime.stderr</string>
    <key>StandardOutPath</key>
    <string>/tmp/workaroundAgainstJumpingTime.stdout</string>
    <key>StartInterval</key>
    <integer>30</integer>
</dict>
</plist>

标签
工作区反跳时间
程序参数
苏多
更新
-u
de.pool.ntp.org
运行负荷
标准错误路径
/tmp/workaroundAgainstJumpingTime.stderr
标准门诊
/tmp/WorkaroundaingInstallJumpingTime.stdout
星际旅行社
30

Charles Duffy,如果您根据您的注释“您的代码只有在上一个命令完成后才能继续运行”进行回答,我会将其标记为已接受的答案。

感谢所有建议。正如Charles Duffy提到的,我的代码只有在前一个命令完成后才会继续。我用以下方法对其进行了测试:

#!/bin/bash -e

while true; do
    echo "start ntpdate"
    sudo ntpdate -u de.pool.ntp.org >> ntpdate.txt; 
    echo "going to sleep"
    sleep 30;
done
所以很明显,睡眠计时器太低,无法运行到限速响应。也许我得提高睡眠时间

正如chepner所建议的,我创建了一个LaunchAgent来处理此问题:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>workaroundAgainstJumpingTime</string>
    <key>ProgramArguments</key>
    <array>
        <string>sudo</string>
        <string>ntpdate</string>
        <string>-u</string>
        <string>de.pool.ntp.org</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/tmp/workaroundAgainstJumpingTime.stderr</string>
    <key>StandardOutPath</key>
    <string>/tmp/workaroundAgainstJumpingTime.stdout</string>
    <key>StartInterval</key>
    <integer>30</integer>
</dict>
</plist>

标签
工作区反跳时间
程序参数
苏多
更新
-u
de.pool.ntp.org
运行负荷
标准错误路径
/tmp/workaroundAgainstJumpingTime.stderr
标准门诊
/tmp/WorkaroundaingInstallJumpingTime.stdout
星际旅行社
30

Charles Duffy,如果您对您的注释做出了回答,“您的代码只有在上一个命令完成后才继续运行。”我会将其标记为已接受答案。

您可以尝试
kill%1
pkill-f test.sh-u$USER
而不是复杂的grep/grep/awk内容。您的构建过程不必担心设置时间。将VM配置为通过
launchd
以所需的间隔运行ntpdate,并让生成过程假定时钟正确。只有在上一个命令完成时,您的代码才会继续。。。除非,也许,你有两个独立的构建同时发生,因此运行两个独立的循环…要处理两个独立的构建的情况,请看这应该有帮助。。。您可以尝试
kill%1
pkill-f test.sh-u$USER
而不是复杂的grep/grep/awk内容。您的构建过程不必担心设置时间。将VM配置为通过
launchd
以所需的间隔运行ntpdate,并让生成过程假定时钟正确。只有在上一个命令完成时,您的代码才会继续。。。除非,也许,你有两个独立的构建同时发生,因此运行两个独立的循环…要处理两个独立的构建的情况,请看这应该有帮助。。。这与不首先将流程放在后台,然后在流控制成功之前等待其退出有什么区别?(使
pid
成为数组也有点奇怪——当然,
$pid
在给定数组时计算第一个元素,但是如果只引用第一个元素,为什么不将数据类型保留为字符串?)这与不首先将流程放在后台,然后在流控制成功之前等待其退出有什么区别?(使
pid
成为数组也有点奇怪——当然,
$pid
在给定数组时计算第一个元素,但是如果只引用第一个元素,为什么不将数据类型保留为字符串?)这有很多危险——一个简单的实现可能有两个并发实例,它们都可以看到没有文件,并且同时独立地执行创建行。为了安全起见,应该使用
flock
或使用
O|u create | O|u EXCL
或目录语义的其他工具来执行锁定小心点(安全性稍差,但可移植性稍高)。@CharlesDuffy:当然,简单的锁文件实现并不完美,但我敢打赌它对于OP的实际用例来说已经足够好了。但你是对的,
flock(1)
是一个很好的使用工具。这有很多危险——一个简单的实现可以有两个并发实例,它们都可以看到没有文件,并且在相同的时间独立地进行