Determine what process is listening on a port on Solaris, without lsof

for x in `ptree | awk '{print $1}'`; do pfiles $x | grep ${PORT} > /dev/null 2>&1; if [ x"$?" == "x0" ]; then ps -ef | grep $x | grep -v grep; fi; done 2> /dev/null
Can use lsof, but since it's not part of the base OS, it's not always available.
Sample Output
root@shell02:~# for x in `ptree | awk '{print $1}'`; do pfiles $x | grep -w ${PORT} > /dev/null 2>&1; if [ x"$?" == "x0" ]; then ps -ef | grep $x | grep -v grep; fi; done 2> /dev/null
    root  5386  5331   0   Oct 06 ?           1:29 /lib/svc/bin/svc.configd
    root  6211  6209   0   Oct 06 ?           0:02 /usr/lib/autofs/automountd
    root 17437  6425   0 09:02:56 ?           0:00 /usr/lib/ssh/sshd
    root  6425  5331   0   Nov 10 ?          18:54 /usr/lib/ssh/sshd
    root 17437  6425   0 09:02:56 ?           0:00 /usr/lib/ssh/sshd
     bob 17438 17437   0 09:02:56 ?           0:00 /usr/lib/ssh/sshd
     bob 17438 17437   0 09:02:56 ?           0:00 /usr/lib/ssh/sshd
     bob 17441 17438   0 09:02:57 pts/1       0:00 -sh
  daemon 12227  5331   0   Dec 03 ?           0:00 /usr/lib/nfs/statd
  daemon 12232  5331   0   Dec 03 ?           0:00 /usr/lib/nfs/lockd
root@shell02:~# 

0
By: bpfx
2010-01-05 17:02:23

What Others Think

Sounds good only one problem: NEVER do this on production system. The 'pfiles' command effectively halts the process in order to inspect it. (see the man page). If your process is doing heavy socket i/o while being halted it will crash as a result.
MrKnowItAll · 367 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: