The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
kill multiple instances of the same program ipzig Shell Programming and Scripting 12 12-16-2007 10:42 PM
ypbind - multiple instances starting blowtorch SUN Solaris 0 10-01-2007 03:51 AM
Multiple (thousands) of Cron Instances sysera UNIX for Advanced & Expert Users 10 01-17-2006 05:49 AM
Multiple file instances gozer13 UNIX for Dummies Questions & Answers 1 12-29-2004 03:08 PM
multiple instances of syslogd - is it possible? Gary Dunn UNIX for Advanced & Expert Users 9 07-20-2004 06:10 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-05-2007
Registered User
 

Join Date: Jul 2006
Posts: 145
Stumble this Post!
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-05-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
Stumble this Post!
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:

Code:
#!/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
Reply With Quote
  #3 (permalink)  
Old 04-05-2007
Registered User
 

Join Date: Feb 2007
Location: Boston, MA
Posts: 64
Stumble this Post!
Quote:
Originally Posted by ragha81
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
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.
Reply With Quote
  #4 (permalink)  
Old 04-06-2007
Registered User
 

Join Date: Jul 2006
Posts: 145
Stumble this Post!
thanks a ton for your timely help guys.. i was able to achieve the desired results...
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0