#!/bin/bash
# Tornado Online Backup Version: 5.2.6.0 (Build:8)
# Last Modified: 2008-26-06 02:56:33

# Kill Process Matched the provided Process Image Name

if [ $# = 0 ];then
	echo "Please Specify the Image Name as an argument"
	echo "Usage: taskkill CMD_NAME"
	exit 0
fi

CMD_NAME="$1"
echo "Kill Process by Image Name: $CMD_NAME"

PID_CURRENT_SHELL=`echo $$`

# Find and kill all image name matched the CMD_NAME
# The PID of this shell script will appear at last as sorted by PID

while true ; do
	PID_FOUND=`ps ax -opid,command | grep "$CMD_NAME" | grep -v grep | head -n 1 | xargs -n 1 echo | sed '/^ *$/d' | head -n 1`;
	if [ "$PID_CURRENT_SHELL" = "$PID_FOUND" ]; then
		break;
	elif [ "" = "$PID_FOUND" ]; then
		break;
	else
		echo "Kill process of PID ${PID_FOUND}"
		kill -9 $PID_FOUND
	fi
done