Preventing to Bash script from running in parallel or overlap using cron

后端 未结 3 1717
谎友^
谎友^ 2021-01-28 15:27

If i have the following entries in my cron table:

00 03 * * * /java_prog1.sh 
00 5 * * * /java_prog2.sh

The first job usually takes around 30 m

3条回答
  •  梦谈多话
    2021-01-28 16:08

    The creation of directories is atomic, so you could add this in both scripts:

    lock_dir=/tmp/name_of_lock_directory
    if ! mkdir "$lock_dir"; then
        echo "Cannot run $0: lock directory $lock_dir exists" >&2
        exit 1
    fi
    cleanup () { rm -r "$lock_dir"; }
    trap cleanup EXIT
    # if you want, you can add the running pid to a file in the lock directory
    echo $$ > "$lock_dir/pid"
    

提交回复
热议问题