Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check whether port is used

Posted on 2006-07-24
3
Medium Priority
?
391 Views
Last Modified: 2013-12-27
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.
0
Comment
Question by:yongsing
  • 3
3 Comments
 
LVL 16

Accepted Solution

by:
Hanno P.S. earned 200 total points
ID: 17167907
a short
  netstat -an | grep 8001
should show you
0
 
LVL 16

Expert Comment

by:Hanno P.S.
ID: 17167922
Status like idle, listen, established shwo that somthing bound to it already.
0
 
LVL 16

Expert Comment

by:Hanno P.S.
ID: 17168077
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
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question