bash脚本:自上次运行后等待几秒钟
如何编写自上次运行以来将其命令的执行延迟几秒钟的bash脚本?bash脚本:自上次运行后等待几秒钟,bash,Bash,如何编写自上次运行以来将其命令的执行延迟几秒钟的bash脚本? 我的意思是将最后一次执行时间存储在某个地方,计算与当前时间的差异,然后在最后一次运行距离不到5秒时休眠,比如说5秒。如果有人需要它: timeout=5 timefile=/tmp/dc-time.info if [[ ! -e $timefile ]]; then touch $timefile fi old=`stat $timefile --format=%Y` touch $timefile new=`stat $t
我的意思是将最后一次执行时间存储在某个地方,计算与当前时间的差异,然后在最后一次运行距离不到5秒时休眠,比如说5秒。如果有人需要它:
timeout=5
timefile=/tmp/dc-time.info
if [[ ! -e $timefile ]]; then
touch $timefile
fi
old=`stat $timefile --format=%Y`
touch $timefile
new=`stat $timefile --format=%Y`
count=$((timeout-new+old))
if [ $count -gt 0 ]
then
echo "waiting..."
sleep $count
fi
我要问自己的第一个问题是,“我真的需要延迟这些命令的脚本执行吗?”如果您可以并行运行多个命令,然后等待每个进程使用wait命令完成,这通常更快更可取。看看这个正在讨论的问题 也就是说,如果您真的需要等待至少五秒钟,您可以这样做:
#!/bin/bash
export delay_seconds=5
export start_time=`date +%s`
#Run some commands here...
seconds_since_start=`expr \`date +%s\` - $start_time`
if [ $seconds_since_start -lt $delay_seconds ]; then
time_remaining=`expr $delay_seconds - $seconds_since_start`
echo "Sleeping "$time_remaining" seconds..."
sleep $time_remaining
else
echo $seconds_since_start" seconds have already passed since the commands above started. Continuing..."
fi
echo "Script finished."
touch somefile
将somefile
的时间戳更新为当前时间stat——format=%Y somefile
以整数形式提供文件的时间戳(如果您有GNU stat)。你只需要这两个命令,不是吗?(如果您需要防止多个并发调用,那么事情会变得更加棘手,您开始希望使用flock
进行锁定)。您为什么还要这样做?它闻起来像是一种味道。可能还有其他解决方案,例如:(a)在脚本末尾睡觉;(b) 让父进程睡眠;(c) 根本不睡觉;(d) 使用cron;(e) 使用手表。我对詹金斯的docker compose有问题。当使用docker compose构建时,这是一个记录在案的错误,会导致重复的容器错误。因此,我使用此修复程序在管道中使用docker compose