Link to home
Start Free TrialLog in
Avatar of yongsing
yongsing

asked on

Check whether port is used

We are using a report server that listens on port 8001. If we start the report server (through a script) a second time, it will fail because port 8001 is already taken up by the first instance. How can I modify my script to check that the port is already taken up, so that I won't start the server again?

I can use telnet, but it's going to prompt me to enter something. All I need is to check that the server is listening on that port.
ASKER CERTIFIED SOLUTION
Avatar of Hanno P.S.
Hanno P.S.
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Status like idle, listen, established shwo that somthing bound to it already.
If you are looking for a certain port for a given process ID (pid) use
# pfiles $pid | grep "sockname:" | grep "port:" | awk '{print $NF}'`

If you need a report for ALL ports, run this script:
#!/bin/ksh
for pid in `ps -ef -o pid | tail +2` ; do
 port=`pfiles $pid 2>&1 | grep "sockname:" | grep "port:" | awk '{print $NF}'` > /dev/null
 [ ! -z "$port" ] &&  echo "`pfiles $pid 2>&1 | grep \"^$pid:\"` => `echo $port`"
done

JustUNIX