Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a legacy application (that I do not control) that occasionally crashes without properly closing the ports it is listening on. Is there a Solaris command to stop listening on a given port, short of rebooting the box?

share|improve this question
up vote 2 down vote accepted

Well. You need to find the process that's gone zombie, or has ceased being usable, to find the port that's still open. If you know the process name or PID, that's very helpful to use with commands like pfiles, preap, and pkill.

I have a script that can search PIDs or process-names for open ports.

#!/usr/bin/ksh -p


# if $1 is all numbers, then assume it's a PID, and get the parent process.
if [[ $process = +([0-9]) ]]; then
        for pid in $( ptree $process | grep '^[0-9]' | awk '{print $1}' ); do
                pfiles $pid | egrep "^$pid:|sockname"
# if $1 is/has letters, then assume it's a process name, and grep accordingly.
        for pid in $( ptree | grep $process | grep '^[0-9]' | awk '{print $1}' ); do
                pfiles $pid | egrep "^$pid:|sockname"

That will print the PID, processname, and any sockets open for any process that matches $1.

Once you know exactly which PID is holding that socket open, and not being usable, you can use one of the pkill or preap commands to forcibly close that PID and the file descriptors it has open.

share|improve this answer
Whoops. Just be aware that the execution of pfiles command will shortly halt the process. I've seen processes crash because of somebody executing the pfiles command on them. It is rare that this will happen but you should be prepared for this. You can install 'lsof' as described elsewhere on this page but it uses an undocumented hack into Solaris afaik. I believe that with Solaris 11 onwards you potentially have other options through the Dtrace tcp provider. – peterh Dec 6 '12 at 17:58

If the post is still in the listening state then the application is still somehow running. Use the command lsof -i to check which process is still listening and kill it.

If you don't have the lsof tool installed, you have to search the ps -ef for still running processes of your application.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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