Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-03-2005, 09:42 PM   #1
LQ Newbie
Registered: May 2005
Location: canada
Distribution: SuSE 8.0
Posts: 9

Rep: Reputation: 1
Thumbs down verify if the same shell script is executing in background

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}" != "$$" ]
echo "Another instance of the script <script_daemon> is running, close it !"
exit 1
by adding that, i can never execute any session of the script ....

Old 08-04-2005, 12:31 AM   #2
Registered: Nov 2004
Location: India
Distribution: RedHat, PCQLinux, Fedora
Posts: 95

Rep: Reputation: 15
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
pidof -x script_name
Old 08-07-2005, 05:40 AM   #3
Registered: May 2005
Posts: 378

Rep: Reputation: 33
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
echo $$ >/var/
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.

Last edited by eddiebaby1023; 08-07-2005 at 05:42 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
executing konsole from shell script Meriadoc Linux - Newbie 10 07-10-2009 08:08 AM
Executing a shell script command!! vishamr2000 Programming 4 08-15-2005 12:33 AM
Auto-Executing Shell Script on boot benrose111488 Linux - Software 5 12-18-2004 12:52 AM
executing shell script on apache nooodles Linux - General 1 06-29-2004 01:51 AM
executing a shell script from C llama_meme Programming 2 04-01-2002 09:10 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:30 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration