同時に複数回呼び出される可能性のあるbashスクリプトがあります。スクリプトがアクセスする(ファイルに保存されている)状態情報を保護するために、次の/tmp
ようなファイルロックを使用しています。
do_something()
{
...
}
// Check if there are any other instances of the script; if so exit
exec 8>$LOCK
if ! flock -n -x 8; then
exit 1
fi
// script does something...
do_something
これで、このスクリプトの実行中に呼び出された他のインスタンスがすべて終了します。n回ではなく、n回の同時呼び出しがあった場合、スクリプトを1回だけ余分に実行したいのですが、次のようになります。
do_something()
{
...
}
// Check if there are any other instances of the script; if so exit
exec 8>$LOCK
if ! flock -n -x 8; then
exit 1
fi
// script does something...
do_something
// check if another instance was invoked, if so re-run do_something again
if [ condition ]; then
do_something
fi
どうすればこれを行うことができますか?終了する前に群れの中のファイルに触れて、うまくいかない場合は、そのファイルを2番目の条件として使用します。