How to check if another instance of the process is running | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

How to check if another instance of the process is running

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-28-2005
sim sim is offline
Registered User
 
Join Date: Jun 2005
Last Activity: 13 March 2007, 9:35 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
How to check if another instance of the process is running

Hi,

I am writing a shell script to invoke a C++ program. Before I start the C++ program (oi7loadbalancer), I am checking if the process is already running. I start the process only if it is not already running. I have the following check in my script.

proccount=`ps -f -u $USER_NAME | grep "oi7loadbalancer" | grep -v "grep" | wc -l`

If proccount is equal to zero, i will start the process and otherwise not. It is working fine in most cases. But in some cases, when multiple instances of the script are invoked, they do the checking at the same time and both the scripts call the C++ program.

I want to prevent this. Please help me.

I also tried to add a sleep of 2 seconds before this check. But that also did not help. Can i use $RANDOM value as parameter to the sleep command? will this $RANDOM also return the same number to both the instances of the script, when invoked at exactly the same time? Also, i want the sleep time to be a random number between 0 and 5. how can i do this.

Thanks in advance.
Sim
Sponsored Links
    #2  
Old 06-28-2005
vino's Avatar
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 9 April 2013, 8:19 AM EDT
Location: Sydney, Down Under
Posts: 2,847
Thanks: 0
Thanked 12 Times in 12 Posts
Why dont you use a lock ? Once you enter the script, enable the lock and proceed ahead.

Check out google's post on Run Processes Sequentially

Vino
Sponsored Links
    #3  
Old 06-28-2005
sim sim is offline
Registered User
 
Join Date: Jun 2005
Last Activity: 13 March 2007, 9:35 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Vino,

I tried using a temporary file with pids and creating a lock directory to check for another process instance. But in my situation, there is one more complication. The script which is invoking the C++ program exits even before the other process is completed. (Actually, this script is invoking the C++ executable using another script. this second script invokes the C++ program in the background and do not wait for the it to finish.) Hence, even if the lock file is deleted, the C++ program may be running.

Regards,
Sim
    #4  
Old 06-28-2005
vino's Avatar
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 9 April 2013, 8:19 AM EDT
Location: Sydney, Down Under
Posts: 2,847
Thanks: 0
Thanked 12 Times in 12 Posts
Creating locks based on pids would'nt help. Each process that starts off, gets a new pid.

You mention that multiple scripts are involved. This is the chain of commands that I understood.

script1 -> script2 -> native code

i.e. script1 does something, calls script2, does something and then your app. is called.

This is what you should do.

Script1 checks for 'no ps count' and 'no lock file'. Hence your app. is not running. Create a lockfile and start script2.

Script2 checks for lock file and 'starts your app.' and 'deletes the lockfile'.

Now when you run script1 again, it will fail because will find 'ps count'.

Try it out.

Hope I didnt make any fatal flaws/assumptions.

'no ps count' is your original ps command.

vino
Sponsored Links
    #5  
Old 06-28-2005
sim sim is offline
Registered User
 
Join Date: Jun 2005
Last Activity: 13 March 2007, 9:35 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
your assumptions are correct. But i doubt if this will solve my problem because i had problems only when 2 instances of script1 were starting at the same instance (timestamp is same). according to what i understood, in this case also, both the instances can do the ps check and lock file creation at the same time (if file is created using touch command, it will not give any error even if the file exist). and two instances of the script 2 will be started and then the C++ program. Please correct me if i am wrong.
Sponsored Links
    #6  
Old 06-28-2005
vino's Avatar
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 9 April 2013, 8:19 AM EDT
Location: Sydney, Down Under
Posts: 2,847
Thanks: 0
Thanked 12 Times in 12 Posts
Create lock a lock file as follows in your script2.


Code:
[ ! -f lockfile ] && >lockfile || exit 1

This will check whether the files exists or not and then creates a lock file or else fail.

Vino
Sponsored Links
    #7  
Old 06-29-2005
sim sim is offline
Registered User
 
Join Date: Jun 2005
Last Activity: 13 March 2007, 9:35 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you very much. It is working
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
need to check if the process is running shifahim Shell Programming and Scripting 8 08-18-2011 10:17 PM
How do I check if a process is running in C cprogdude Programming 1 10-12-2010 05:01 AM
How to check if process is running? ladyAnne UNIX and Linux Applications 5 05-01-2010 01:20 AM
Check if Process is running Raynon Shell Programming and Scripting 3 01-08-2010 02:03 AM
check process running rose1207 Shell Programming and Scripting 4 12-28-2007 12:23 AM



All times are GMT -4. The time now is 01:55 AM.