aboutsummaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2023-03-29 09:19:30 -0300
committerEuAndreh <eu@euandre.org>2023-03-29 09:19:35 -0300
commit26084f1608ea037bb524fe0e60bd06098fd812d2 (patch)
treeb8dcb948fd44a52dcbed9941cf0ec5c559e77887 /bin
parentetc/guix/home.scm: Add "fakeroot" package (diff)
downloaddotfiles-26084f1608ea037bb524fe0e60bd06098fd812d2.tar.gz
dotfiles-26084f1608ea037bb524fe0e60bd06098fd812d2.tar.xz
bin/free-port: Try random ports over sequential ones
To diminish race-conditions where two separate processes call free-port(1) and get the same port that is free at that time, but only one is able to bind to it.
Diffstat (limited to 'bin')
-rwxr-xr-xbin/free-port9
1 files changed, 7 insertions, 2 deletions
diff --git a/bin/free-port b/bin/free-port
index 2476b4b..1c2af61 100755
--- a/bin/free-port
+++ b/bin/free-port
@@ -61,13 +61,18 @@ done
shift $((OPTIND - 1))
-PORT=1024
+MIN_PORT=1024
+MAX_PORT=65536
+
+gen_port() {
+ shuf -n1 -i "${MIN_PORT}-${MAX_PORT}"
+}
while true; do
+ PORT="$(gen_port)"
if ! lsof -s TCP:LISTEN -i ":$PORT" >/dev/null; then
break
fi
- PORT=$((PORT + 1))
done
echo "$PORT"