本文介绍了Bash脚本等待(如果已在运行),然后继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bash脚本,程序完成后会被程序调用.我需要一种让bash脚本等待自身其他实例正在运行退出的方式,然后再继续.我不能只使用锁定文件并退出,因为不会按任何常规时间表再次调用该脚本.

I have a bash script that is called by a program once a process is complete. I need a way for that bash script to wait if another instance of itself is running to exit before continuing. I can't just use lock files and exit because the script will not be called again on any kind of regular schedule.

推荐答案

在脚本中,在创建锁定文件之前,您可以循环检查锁定文件是否存在,如果存在,则继续循环(通过 sleep 命令或其他方式)以及何时消失,创建它.

Within the script, before creating the lock file, you can loop through checking if the lock file exists, if it does, continue looping (through a sleep command or something) and when it goes away, create it.

类似的东西:

#!/bin/bash

while [ -e /var/tmp/bash_script.lock ];
do
    sleep 5
done

touch /var/tmp/bash_script.lock

echo "do something"

rm /var/tmp/bash_script.lock

这篇关于Bash脚本等待(如果已在运行),然后继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:54