Source: killclosewait.phps - download
<?php
/* 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * killclosewait.php 0.1
 * by daif alotaibi (http://daif.net)
 * daif@daif.net
 * 
 * Link:
 *  http://daif.net/script/killclosewait.phps
 * 
 * Description:
 *  kill all connections that are in CLOSE_WAIT state by PORT number.
 *  you can check how many connections in CLOSE_WAIT state by this command:
 *  netstat -an|awk '/tcp/ {print $6}'|sort|uniq -c
 *  run this script with root privilege as corn job.  
 * 
 * Changelog
 * 0.1    first release
*/

    
$host    'daif.net';        // hostname or server ip
    
$port    80;                // port number 
    
$timeout 5;                // time out in sec 
    
$fp fsockopen($host$port$errno$errstr$timeout);
    if (!
$fp) {
        echo 
shell_exec("netstat -anp | grep ':$port ' | grep CLOSE_WAIT | awk '{print $7}' | cut -d \/ -f1 | grep -oE \"[[:digit:]]{1,}\" | xargs kill");
    }
    @
fclose($fp);
?>