Merge branch 'master' of mathias-kettner.de:omd
[omd.git] / packages / omd / port_is_used
blob70ade66e4ef6610ad30673ae91374f1dd5737cac
1 #!/bin/bash
3 # return next free port for key
5 # usage: $OMD_ROOT/lib/port_is_used APACHE_TCP_PORT 5000
8 port_is_used () {
9 local KEY=$1
10 local PORT=$2
11 for S in $(ls /omd/sites); do
12 if [ ! -d /omd/sites/$S ]; then
13 continue
15 conf=/omd/sites/$S/etc/omd/site.conf
16 if [ "${S}" = "${OMD_SITE}" ]; then
17 if [ $(env | grep ^CONFIG_ | grep "=${PORT}$" | grep -v CONFIG_${KEY} | wc -l) -gt 0 ]; then
18 return 0
20 else
21 if ! ls $conf >/dev/null 2>&1; then
22 echo "ERROR: Failed to read config of site ${S}. ${KEY} port will possibly be allocated twice" >&2
23 continue
25 if [ $(grep -v ^# < $conf | grep "='${PORT}'" | wc -l) -gt 0 ]; then
26 return 0
29 done
30 return 1
33 KEY=$1
34 PORT=$2
35 while port_is_used "$KEY" "$PORT"; do
36 PORT=$((PORT + 1))
37 done
38 echo "$PORT"