LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

Rep: Reputation: 0
Thumbs down 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!
 
Old 08-04-2005, 01:31 AM   #2
hk_linux
Member
 
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
Code:
pidof -x script_name
HTH
 
Old 08-07-2005, 06:40 AM   #3
eddiebaby1023
Member
 
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
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.

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


Reply


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 09:08 AM
Executing a shell script command!! vishamr2000 Programming 4 08-15-2005 01:33 AM
Auto-Executing Shell Script on boot benrose111488 Linux - Software 5 12-18-2004 01:52 AM
executing shell script on apache nooodles Linux - General 1 06-29-2004 02:51 AM
executing a shell script from C llama_meme Programming 2 04-01-2002 10:10 PM


All times are GMT -5. The time now is 12:23 AM.

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