Skip to content

Instantly share code, notes, and snippets.

@KiNgMaR
Created April 21, 2021 10:29
Show Gist options
  • Save KiNgMaR/4be570682feecc29db20d661f0628334 to your computer and use it in GitHub Desktop.
Save KiNgMaR/4be570682feecc29db20d661f0628334 to your computer and use it in GitHub Desktop.
Bash script that atomically selects a port from the range 30000-40000 on Linux
# ref: https://unix.stackexchange.com/a/274499
#
RANDOM_PORT=$(expr `(flock -x 200; cat /tmp/nextport; expr \( \`cat /tmp/nextport 2>/dev/null\` + 1 \) % 10000 > /tmp/nextport;) 200>/tmp/nextport.lock` + 30000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment