|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
Why dont you use a lock ? Once you enter the script, enable the lock and proceed ahead.
Check out google's post on http://www.unix.com/shell-programming-scripting/18137-run-processes-sequentially.html Vino |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Thank you very much. It is working
![]() |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|