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
Multiple instances of a job. Gangegowda Shell Programming and Scripting 1 10-16-2008 05:22 AM
multiple instances of same vg on same AIX machine navadeep AIX 1 07-25-2008 02:58 AM
detecting multiple instances ragha81 Shell Programming and Scripting 3 04-06-2007 11:09 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-03-2009
raghu.amilineni
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
Checking for multiple instances of a process

Hi

I have a scenario where i need to check multiple instances of a running shell script (abc.sh) .

How can I find from inside a running shell script whether any other instance of the same script is running or not? If any other instance of same shell script is running I need to exit from the current script.

I am using /usr/bin/ps -eaf | grep abc to check whether the script "abc" is running or not.

Thanks in advance
Raghu
  #2 (permalink)  
Old 04-03-2009
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Quote:
Originally Posted by raghu.amilineni View Post
Hi

I have a scenario where i need to check multiple instances of a running shell script (abc.sh) .

How can I find from inside a running shell script whether any other instance of the same script is running or not? If any other instance of same shell script is running I need to exit from the current script.

I am using /usr/bin/ps -eaf | grep abc to check whether the script "abc" is running or not.

Thanks in advance
Raghu
You could do something like...


Code:
cnt=$(/usr/bin/ps -eaf | grep [a]bc.sh)
if [[ $cnt -gt 1 ]]; then
  echo "Another instance detected"
  exit 1
fi;

  #3 (permalink)  
Old 04-03-2009
raghu.amilineni
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
thanks
  #4 (permalink)  
Old 04-03-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
look into 'man fuser'.
  #5 (permalink)  
Old 04-04-2009
dennis.jacob dennis.jacob is offline Forum Advisor  
dj - the student
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 610
You can add a line at the beginning ot handle this, such as..
Quote:
CUR_PID=$$;
ps -ef| grep -vw $CUR_PID | awk ' { print $2 }' | xargs kill -9
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 12:58 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