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


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

#! /bin/bash

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

    flock -n 9 || exit 1
    # ... commands executed under lock ...
) 9>/var/lock/"$category"
  • 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.