LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   verify if the same shell script is executing in background (https://www.linuxquestions.org/questions/programming-9/verify-if-the-same-shell-script-is-executing-in-background-349768/)

hicham007 08-03-2005 09:42 PM

verify if the same shell script is executing in background
 
hallo,
Because if i execute many session of the same shell script cause some bugs,
i would like to add some kind of verification in the head of that script, in order to forbid the user to execute another session of the same script, and exit with a message like "there is another session of the script on execution, close it before continuing!!"

How to do that ?
i guess that i have to use the ps command but how?
i tried that but doesn't work :(

*********************************
instance=`ps -A | grep reply_daemon | awk '{print $1}'`
if [ "${instance}" != "$$" ]
then
echo "Another instance of the script <script_daemon> is running, close it !"
exit 1
fi
**************************************
by adding that, i can never execute any session of the script ....:)


thanks!

hk_linux 08-04-2005 12:31 AM

I faced a simliar problem some time back.
What you got to do is, have a wrapper script which will check for the existance of the actual script.
In the wrapper you can provide interfaces for starting, stopping, restarting the script.

Alternate for ps, you can also use
Code:

pidof -x script_name
HTH

eddiebaby1023 08-07-2005 05:40 AM

Get your script to create a file in a known place (/var?), containing its PID. When you start your script, look for the file, if it's there check for a process with the same PID as the file contents (kill -0 otherpid and check for a 0 (true) result) - if it's there, your script is already running and you can exit gracefully. If it's not there
Code:

echo $$ >/var/myscript.pid
and you've locked other instances out. There's a small risk of a race condition if you start two instances at precisely the same time, but solve that only when you really need to.:)


All times are GMT -5. The time now is 11:54 AM.