2

if I have a deployment.sh, it has two parameters: environment and release version.

deployment.sh uat release1
deployment.sh prod release1

I want to make at any time: the deployment.sh with same environment and release version can be run in singleton. But deployment.sh uat release1 and deployment.sh prod release1 can be run concurrently.

How to do this. Thx

1
5

One way of doing this is file locking which is explicitly for picking just one among several processes. See man flock:

#! /bin/bash

category="$1"
if [ 'uat' != "$category" ] && [ 'prod' != "$category" ]; then
    exit 2
fi

(
    flock -n 9 || exit 1
    # ... commands executed under lock ...
) 9>/var/lock/"$category"
1
  • flock is the most reliable solution I know too in this use case – Gilles Quenot Jun 29 '20 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.