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! |
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 |
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 |
All times are GMT -5. The time now is 11:54 AM. |