From 51b8860d23dd16bd51d6e87a0d13d2bb6ae27073 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Wed, 23 Mar 2016 14:20:52 -0400 Subject: [PATCH] catalyst-auto: add an option to hold a lock while running This makes it easy to put into a cronjob and not worry about a copy already/still running. --- tools/catalyst-auto | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tools/catalyst-auto b/tools/catalyst-auto index 0b1c3894..75f6a84a 100755 --- a/tools/catalyst-auto +++ b/tools/catalyst-auto @@ -26,6 +26,7 @@ keep_tmpdir=0 testing=0 preclean=0 lastrun=0 +lock_file= # Set pipefail so that run_cmd returns the right value in $? set -o pipefail @@ -48,6 +49,7 @@ Options: -k|--keep-tmpdir Don't remove temp dir when build finishes -t|--test Stop after mangling specs and copying files --interval Exit if last successful run was less than ago + -l|--lock File to grab a lock on to prevent multiple invocations -h|--help Show this message and quit EOH @@ -130,6 +132,10 @@ do lastrun=$1 shift ;; + -l|--lock) + lock_file=$1 + shift + ;; -*) usage "ERROR: You have specified an invalid option: ${a}" exit 1 @@ -137,6 +143,15 @@ do esac done +( + +if [[ -n ${lock_file} ]]; then + if ! flock -n 9; then + echo "catalyst-auto already running" + exit 1 + fi +fi + # Probe the default gitdir from this script name. GITDIR=$(dirname "$(dirname "$(realpath "$0")")") @@ -331,3 +346,5 @@ if [ ${build_failure} = 0 ]; then else send_email "Catalyst build complete, but with errors" "Build process has completed, but there were errors. Please consult previous emails to determine the problem." fi + +) 9>"${lock_file:-/dev/null}"