bash flock: exit if can't acquire lock

后端 未结 2 1335
孤街浪徒
孤街浪徒 2020-12-09 04:26

The following lock mechanism is used for preventing a cron job from running concurrently:

#!/bin/bash

echo \"Before critical section\"
(
    fl         


        
2条回答
  •  有刺的猬
    2020-12-09 04:45

    flock -n -e 200 || exit 1
    

    flock -n tells you it failed by returning a failure code (something other than zero). You could instead do set -e at the top of your script to make it exit when it sees any unchecked error.

    Depending on your application, you might want to exit 0 to indicate success when the lock can't be acquired.

提交回复
热议问题