nerdcred++
Well, once again, I needed to find a solution to a non-existent problem. Well, almost non-existent.
The other day we had a rather unique error when trying to restart an Apache process. A stop command was issued, the process died off in the process list, but an orphaned session was keeping the http port tied up, so Apache could not restart.
What to do?
Well, given my awakening and increasing knowledge of the various commands and utilities in Linux, the only solution was to over-engineer a fully automated solution I could incorporate into a maintenance script.
With no further adieu, I give you a completely hands off way to kill the process holding on to a given port.
sudo -S kill -9 `sudo -S netstat -lpn < $SFL | grep :80 | awk ' { split($7,pid,"/"); printf("%s",pid[1]) } '` < $SFL
$SFL is the absolute location of a file containing the sudo password of the account used to run the command(s).
Fairly simple, and overly engineered solution, so the only proper title was of course, nerdcred++. Anyone else have a more elegant solution that can be used in an automated script, and without the httpd.pid being in existence?
May 30th, 2010 at 7:21 pm
The only thing I would suggest is to collapse the grep into the awk:
awk ‘/:80/{split($7,p,”/”);printf(“%s”,p[1])}’
Saves on some execution time, and a pipe.
I also prefer using $() instead of backticks, since there are fewer nesting issues if you go to much more ridiculous one-liners. That being said, helper functions in an included .sh for init scripts would be an excellent place to put a nice, multiline script version that is readable and maintainable if problems arise.
Interesting problem, interesting solution. Do you then poll netstat to make sure the port is free?
June 2nd, 2010 at 8:09 am
Yep, this is nested in a loop, and executes if the port isn’t cleared up after a minute of issuing the httpd stop command.