Block execution of script while same script is already running

M

MrBanabas

Hi,

Does anybody no how I may achieve it:

Bob is starting script my_test.rb which runs a over an hour...
5 minutes later...
Alice is not knowing that Box has already started is and starts it
again.
Script should detect that it is already running and stop executing.

What is the best way to achieve this?

Thanks a lot.
 
R

Robert Klemme

2008/12/17 [email protected] said:
Bob is starting script my_test.rb which runs a over an hour...
5 minutes later...
Alice is not knowing that Box has already started is and starts it
again.
Script should detect that it is already running and stop executing.

What is the best way to achieve this?

There are solutions for this in the archives of this list. You can
use an exclusive file lock on $0 as an example.

Kind regards

robert
 
S

Sean O'Halpin

Hi,

Does anybody no how I may achieve it:

Bob is starting script my_test.rb which runs a over an hour...
5 minutes later...
Alice is not knowing that Box has already started is and starts it
again.
Script should detect that it is already running and stop executing.

What is the best way to achieve this?

Thanks a lot.

Daniel Berger suggested the following technique (see thread starting
at ruby-talk:302489).

This is my version:

def single_instance(&block)
if File.open($0).flock(File::LOCK_EX|File::LOCK_NB)
block.call
else
warn "Script #{ $0 } is already running"
end
end

single_instance do
# your main code here
end

Regards,
Sean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top