comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:22f22c3e81bf
1 #!/bin/bash
2
3 set -e
4
5 # This script finds a free port on the local machine in the 7000-65000 interval
6 # Should work in biocontainers derived from conda
7 # Taken from https://unix.stackexchange.com/a/358101
8
9 if command -v ss &> /dev/null
10 then
11 ss -alnut | awk '
12 $2 == "LISTEN" {
13 if ($5 ~ "[.:][0-9]+$") {
14 split($5, a, /[:.]/);
15 port = a[length(a)];
16 p[port] = 1
17 }
18 }
19 END {
20 for (i = 7000; i < 65000 && p[i]; i++){};
21 if (i == 65000) {exit 1};
22 print i
23 }
24 '
25 elif command -v netstat &> /dev/null
26 then
27 netstat -aln | awk '
28 $6 == "LISTEN" {
29 if ($4 ~ "[.:][0-9]+$") {
30 split($4, a, /[:.]/);
31 port = a[length(a)];
32 p[port] = 1
33 }
34 }
35 END {
36 for (i = 7000; i < 65000 && p[i]; i++){};
37 if (i == 65000) {exit 1};
38 print i
39 }
40 '
41 else
42 echo "This tool requires one of 'netstat' of 'ss' commands."
43 fi