34

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.

3 Answers 3

44

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

23
  • 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, 2014 at 3:37
  • What is the output of netstat -tulpn | grep :8080? Aug 11, 2014 at 3:39
  • 1
    @Lawrence one more thing, if I run this command lsof -i -P -sTCP:LISTEN | grep 8080 as a user who started the test_server then it shows the open port on my console properly but if I run that command as a different user, then it doesn't work? Is it normal behavior? Since with netstat it works fine with both the user's.
    – david
    Aug 11, 2014 at 6:45
  • 1
    @user2809564 lsof will only be able to get ports opened by the user that is running lsof. If you need to run it as a different user, user sudo lsof
    – Lawrence
    Aug 11, 2014 at 6:48
  • 1
    Good, that's much cleaner in matching what you really want, compared to using grep on output. Maybe it was related to which user can see which, let's check... Oh, indeed, it lists only own listening processes by default. That explains much. Aug 11, 2014 at 16:21
25

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 not open"

Replace the port with what you are testing.

Or you can use nc.

nc -vz 127.0.0.1 8080

Returns:

Connection to 127.0.0.1 8080 port [tcp/*] succeeded

3
  • 2
    nc is a great solution for my use case; thanks for suggesting
    – Jon
    Sep 19, 2018 at 22:43
  • Shouldn't the echo solution employ the -n option? Otherwise echo would probably not only establish a connection, but also send a newline. Am I wrong?
    – x-yuri
    Dec 17, 2020 at 18:29
  • 1
    nc is the best solution for cross platform compatibility.
    – miu
    Aug 16, 2021 at 5:06
6

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.