Linux:ClearPort

From Devicenull's Code

Jump to: navigation, search

This was written in response to Enemy Territory servers freezing up and not properly releasing the port they were listening on. The next instance would then start on a different port causing all sorts of issues. It was designed to work in a restricted environment, where "netstat -anp" would not return PID's due to permission issues. It works around this by looking for the "net_ip x.x.x.x" line in the command line of the server. It's been tested for a few months now, and works perfectly.

#!/bin/bash
 
# Brian Rak - 2008
 
if [ $# -lt 2 ]
then
	echo 'Usage: clearport.sh ip gamebin'
	echo 'ex: clearport.sh 127.0.0.1 etded.x86'
	exit 1
fi
 
PID=`ps -o pid,comm,args -u myuser -w -w | grep -i "$2" | grep -i "net_ip $1" | awk '{print $1}'`
 
if [ -n "$PID" ]
then
	for cur in $PID
	do
		echo "Hung server process found, PID=$cur..."
 
		kill -s 9 $cur
 
		if [ $? -eq 0 ]
		then
			echo 'Process killed sucessfully'
		fi
	done	
fi
Personal tools