detecting multiple instances

Hi Gurus

I have a requirement like this. i use solaris OS..

if there are 2 instances of the same ksh file running in the directory, i need to kill the ksh file that started to run latest.

suppose ragha.ksh starts running thru cron in abc/xyz directory
now ragha.ksh started running by any of these(either thru cron or by manually running it)

I need to see the one that started latest and kill it and allow the older one to run

how do i go about this requirement..

thanks in advance

here's the paradigm I usually use on Solaris of detecting another instance of the same running script. If another instance IS detected, the current (the calling instance) will exit allowing the 'older' instance to finish its work.

It's not exactly what you want, but might be a good starting point:

#!/bin/ksh
thisFILE="$(whence ${0})"

progNameFull="${0##*/}"
progName="${progNameFull%%.*}"

AWK='/bin/nawk'
FUSER='/usr/sbin/fuser'

#----------------------------------------------------------------------
# see if there's another instance of THIS script running - don't allow MULTIPLE
# instances of the same script running at the same time. If there's a previously
# invoked instance of this script running, log an error message and exit THIS
# current invocation of the script - allow the previously invoked script finish
# its own workload.
     myPID="$$"
     FUSERout=$(${FUSER} ${thisFILE} 2>/dev/null)
     # echo "fuser ->[${FUSERout}]"
     typeset -i numProc=$(echo "${FUSERout}" | ${AWK} '{print NF}')
     if [[ "${numProc}" -gt 1 ]] ; then
        echo "${progName}::main Error: another instance(s) of [${thisFILE}] is currently still running [$(echo ${FUSERout} | sed -e 's/  */ /g')] - exiting THIS [${myPID}] run."
        exit 1
     fi

ps -eaf | "grep ragha.ksh"| grep -v grep | sort -k5 -rn | head -1

You will get the process id of the last started process. You can use it to kill the proc.

thanks a ton for your timely help guys.. i was able to achieve the desired results...