Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ksh script question re: hung processes

Status
Not open for further replies.

gols

Programmer
Aug 27, 2001
3
US
I'm looking for a way to detect a hung ftp process running on a unix server. I have the pattern to grep for in the process list and the way we determine if this particular process is "hung" is if the start time is an hour (or more) old. For example, this is the "hung" process: ecadmin 26123 26733 0.0 07:19:56 ?? 0:00.05 /usr/bin/ftp -niv ftp.companyname.com My script will be running periodically - this process started at 07:19:56, so if the time is 08:19:56 or later, I want to kill -9 26123. How can I get the difference between the process start time and the current time or is there some other unix command(s) I can use to list this process if it's over 1 hour old? I can't obviously use the cpu time because it's hung and doesn't increment. Any help would be greatly appreciated...thanks!
 
I suspect an easier way to handle this is to re-define your approach. I suggest the following:

Run the script every hour in a cron job.
Have the script determine the currently running FTP processes.
Have the script retrieve a file containing the running FTP processes from last time the script was run.
See if any of the current processes are in the file and kill them if they are.
Save the currently running FTP processes in a file.
 
we have an existing daemon which runs in the background every 10 minutes that i was supposed to include this check for hung processes in...any other ideas?
thanks much
 
I haven't used Korn shell for a while, but the following should work. Make sure to try out the code before putting in the 'kill'.
Code:
TIME=`date +%H%M%S` # Time in hhmmss
HOURAGO = ((TIME - 10000 )) # Subtract an hour
if [ $HOURAGO -lt 0 ]; then
    # Correct for case if an hour ago was yesterday
    HOURAGO = (( HOURAGO + 240000 ))
fi

LIST=`ps -ef | grep "/usr/bin/ftp"` # change FTP string?
for LINE in "$LIST"; do
    START=`echo $LINE | awk '{print $5}' | awk -F ":" '{print $1$2$3}'` #  
    if [ $START -lt $HOURAGO ]; then
        PROCTOKILL=`echo $LINE | awk '{print $2}'`
        echo "Hung process $PROCTOKILL";
        # put your kill here
    fi
done
 
raider2001
this is close, but the LIST returned with the process info is one long line - it doesn't display as separate lines so the ... for LINE in $LIST command doesn't work... any ideas?
thanks so much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top