semaphore

A shell tool to create counting semaphores, acquire them and release them. This is useful if you want to e.g. run no more than N out of M commands in parallel.

usage

Create a semaphore of size 10 for a job that will fetch URLs in parallel.

semaphore create --name fetch-many-urls --size 10

Then before launching each job, acquire the semaphore:

semaphore acquire --name fetch-many-urls

Do your job, and when you’re done:

semaphore release --name fetch-many-urls

example

#!/usr/bin/env bash

lockname=$(uuidgen)
semaphore create --name ${lockname} --size 2

function fetch_url() {
    local url=${1}
    semaphore acquire --name ${lockname}
    echo "fetching URL ${1}"
    sleep 1
    semaphore release --name ${lockname}
}

for ((i=0; i<=10; i++)); do
    fetch_url "http://url.number.${i}" &
done

wait $(jobs -p)

using it

Grab a release or :

on macOS

brew tap aybabtme/homebrew-tap
brew install semaphore

license

The tool here is MIT. The code is 99.9% powered by a fork of bitbucket.org/avd/go-ipc, which is Apache 2.

GitHub

https://github.com/aybabtme/semaphore