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

Hauke Laging
  • 90,279
Robert Chen
  • 47
  • 1
  • 5
  • See https://unix.stackexchange.com/questions/48505/how-to-make-sure-only-one-instance-of-a-bash-script-runs?rq=1 for more ideas. – Alex Tullenhoff Jun 30 '20 at 03:42

1 Answers1

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"

Hauke Laging
  • 90,279