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 and shell scripting languages 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-17-2007 02:42 AM
ypbind - multiple instances starting blowtorch SUN Solaris 0 10-01-2007 07:51 AM
Multiple (thousands) of Cron Instances sysera UNIX for Advanced & Expert Users 10 01-17-2006 09:49 AM
Multiple file instances gozer13 UNIX for Dummies Questions & Answers 1 12-29-2004 07:08 PM
multiple instances of syslogd - is it possible? Gary Dunn UNIX for Advanced & Expert Users 9 07-20-2004 10:10 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-05-2007
ragha81 ragha81 is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 145
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
  #2 (permalink)  
Old 04-05-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
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

  #3 (permalink)  
Old 04-05-2007
Deal_NoDeal Deal_NoDeal is offline
Registered User
  
 

Join Date: Feb 2007
Location: Boston, MA
Posts: 64
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.
  #4 (permalink)  
Old 04-06-2007
ragha81 ragha81 is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 145
thanks a ton for your timely help guys.. i was able to achieve the desired results...
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:31 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0