stop execution of script with in the script

Hello

i want to incorporate of piece of code in the shell script which checks whether same script is already running or not. If it is already running i want to come out (exit) if its not then continue with the execution of the script.

Any help would be very much appreciated

Thanks

(a) do a >ps -ef | grep script_name | wc -l
If you see three matches, then already running. (#1 is current execution, #2 would be the grep command, and #3 would be another execution)
(b) whenever you start the script, have it >touch script_name.on
Then, you can do a >if [ -e script_name.on ] to see if the file (and thus the process) is already going. Just remember to >rm script_name.on at the end of your program.

Try this:

http://www.unix.com/shell-programming-scripting/22874-script-check-particular-process-alert-if-its-not-running.html\#post92287

Note that you can use the variable $0 within a bash script to get the script's own name.

ShawnMilo

Same idea as the other replies... Don't know what flavor of unix you are on, but on Solaris, ps -fo 'pid= args=' will list processes (for a user) giving only their PID and the argument list (including the script name) this might make it easier to parse through the results and look for your own proc.

I have a perl subroutine that does exactly what you want, though it is a little complex cause it does all kinds of logging, etc if the procname and userID passed to it do not exist, etc. But bottom line is execute a ps, capture the results and count the lines containing it...

Also, as the last reply said, you can set a flag (touch a file) that signals the proc is active (each run must check to see if the file already exists). You can also use semaphors (if your unix supports sys V IPC calls) to do the same thing.... I can send you the perl routine if you want....

quine@sonic.net