24

I am running my below shell script on a machine on which c++ application server is running on port 8080 and in the shell script I am executing an URL and stores the response coming from that URL in DATA variable.

But suppose if the same app server is down, then it will not be able to execute the URL and then it will print out Retrying Again and sleep for 30 seconds and then execute the same url again.

#!/bin/bash

HOSTNAME=$hostname
DATA=""
RETRY=15

echo $HOSTNAME

while [ $RETRY -gt 0 ]
do
    DATA=$(wget -O - -q -t 1 http://$HOSTNAME:8080/beat)
    if [ $? -eq 0 ]
    then
        break
    else
        echo "Retrying Again" >&2

        # restart the server

        let RETRY-=1
        sleep 30
    fi
done

echo "Server is UP"

And here HOSTNAME is the local hostname of the server on which I am running my above shell script.

Problem Statement:-

Now what I am trying to do is, if the server is down, then it will print out Retrying Again so after that I want to check whether port 8080 is opened on $HOSTNAME or not. If not, then it means server is down so I want to restart the server by executing this command and then sleep for 30 seconds as shown above in the shell script.

/opt/app/test/start_stop.sh start

Is this possible to do here in my above shell script?

I am running this shell script on Ubuntu 12.04.

23

The program lsof allows you to check which processes are using which ressources, like files or ports.

To show which processes are listening on port 8080:

lsof -Pi :8080 -sTCP:LISTEN

In your case, you want to test whether a process is listening on 8080 - the return value of this command tells you just that. It also prints the pid of the process.

lsof -Pi :8080 -sTCP:LISTEN -t

If you need just the test, with no output, redirect it to /dev/null:

if lsof -Pi :8080 -sTCP:LISTEN -t >/dev/null ; then
    echo "running"
else
    echo "not running"
fi


If you use multiple host names with multiple IP addresses locally, specify the hostname too like
lsof -Pi @someLocalName:8080 -sTCP:LISTEN

  • Thanks Volker. I tried this lsof -i :8080 -sTCP:LISTEN and I am not getting anything on the console but if I am trying this netstat -tulpn | grep :8080 then I get my process which is running on port 8080 why is it so? – david Aug 11 '14 at 3:37
  • What is the output of netstat -tulpn | grep :8080? – Volker Siegel Aug 11 '14 at 3:39
  • This is what I get with netstat - david@machineA:/home/david$ netstat -tulpn | grep :8080 (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 27530/test_server – david Aug 11 '14 at 3:46
  • Strange, if I try similar things it works. Could be related to IPv6... hmmm. – Volker Siegel Aug 11 '14 at 3:50
  • Is it listed with lsof -iTCP -sTCP:LISTEN | grep 8080? – Volker Siegel Aug 11 '14 at 3:57
16

Simplest way in bash. Test if your port is open.

(echo >/dev/tcp/localhost/8080) &>/dev/null && echo "TCP port 8080 open" || echo "TCP port 8080 close"

Replace the echo with what you want.

Or you can use nc.

nc -vz 127.0.0.1 8080

Returns:

Connection to 127.0.0.1 8080 port [tcp/*] succeeded

  • 1
    nc is a great solution for my use case; thanks for suggesting – Jon Sep 19 '18 at 22:43
1

Elaborating on RJ's answer which notes nc is also useful... Not only is nc useful for quick command-line query

❯ nc -z -v -w5 127.0.0.1 8080
localhost [127.0.0.1] 8080 (http-alt) : Connection refused

but it can be used without -v if the human-readability thing isn't what you're after -- e.g., for use in a script (the exit code will indicate whether the port is open or closed).

❯ nc -z 127.0.0.1 8080

❯ echo $?             
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.