循环脚本执行

Art*_*m Y 2 command-line bash scripts

如何让脚本循环执行?在脚本结束时,我需要再次运行它。Cron 不适合,因为我需要在上一个之后立即运行脚本

Ser*_*nyy 5

任何需要无限循环的编程语言的基本控制结构都是while循环。

while true ; do /path/to/script.sh ; if [ $? -ne 0    ] ; then continue ; else break ; fi ; done
Run Code Online (Sandbox Code Playgroud)

更具可读性的格式是:

while true 
do 
   /path/to/script.sh  # Ensure your script actually outputs exit status
   if [ $? -ne 0    ] ; then  
      continue  # if exit status not 0 ( not success ) , repeat
   else         
       break    # if successful - exit
   fi 
done
Run Code Online (Sandbox Code Playgroud)

当然,您需要确保您的脚本在成功时确实具有等于 0 的返回状态。您可能想也可能不想使用完整路径或./操作符在当前目录中运行脚本,或指定解释器,例如python /my/python/script.py

如有必要,您可以在下一次迭代开始之前添加延迟。为此,您可以在sleep 0.250之后fi但之前放置done