diff find_free_port.sh @ 0:22f22c3e81bf draft

planemo upload for repository https://github.com/galaxy-genome-annotation/galaxy-tools/tree/master/tools/genenotebook commit 12282c16658b37858f49944796fd95515ef0fc0b
author gga
date Wed, 11 Jan 2023 11:49:13 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/find_free_port.sh	Wed Jan 11 11:49:13 2023 +0000
@@ -0,0 +1,43 @@
+#!/bin/bash
+
+set -e
+
+# This script finds a free port on the local machine in the 7000-65000 interval
+# Should work in biocontainers derived from conda
+# Taken from https://unix.stackexchange.com/a/358101
+
+if command -v ss &> /dev/null
+then
+  ss -alnut | awk '
+    $2 == "LISTEN" {
+      if ($5 ~ "[.:][0-9]+$") {
+        split($5, a, /[:.]/);
+        port = a[length(a)];
+        p[port] = 1
+      }
+    }
+    END {
+      for (i = 7000; i < 65000 && p[i]; i++){};
+      if (i == 65000) {exit 1};
+      print i
+    }
+  '
+elif command -v netstat &> /dev/null
+then
+  netstat -aln | awk '
+    $6 == "LISTEN" {
+      if ($4 ~ "[.:][0-9]+$") {
+        split($4, a, /[:.]/);
+        port = a[length(a)];
+        p[port] = 1
+      }
+    }
+    END {
+      for (i = 7000; i < 65000 && p[i]; i++){};
+      if (i == 65000) {exit 1};
+      print i
+    }
+  '
+else
+  echo "This tool requires one of 'netstat' of 'ss' commands."
+fi