[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Block execution of script while same script is already running

MrBanabas@googlemail.com

12/17/2008 9:45:00 AM

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.

--
Volker
2 Answers

Robert Klemme

12/17/2008 10:56:00 AM

0

2008/12/17 MrBanabas@googlemail.com <MrBanabas@googlemail.com>:
> 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

--
remember.guy do |as, often| as.you_can - without end

Sean O'Halpin

12/17/2008 6:04:00 PM

0

On Wed, Dec 17, 2008 at 9:37 AM, MrBanabas@googlemail.com
<MrBanabas@googlemail.com> wrote:
> 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.
>
> --
> Volker
>

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