# Block execution of script while same script is already running

**URL:** <https://www.ruby-forum.com/t/block-execution-of-script-while-same-script-is-already-running/155832>\
**Category:** Ruby\
**Created:** [December 17, 2008, 10:45am UTC](https://www.ruby-forum.com/t/block-execution-of-script-while-same-script-is-already-running/155832 "2008-12-17T10:45:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 17, 2008, 10:45am UTC](https://www.ruby-forum.com/t/block-execution-of-script-while-same-script-is-already-running/155832/1 "2008-12-17T10:45:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 17, 2008, 12:04pm UTC](https://www.ruby-forum.com/t/block-execution-of-script-while-same-script-is-already-running/155832/2 "2008-12-17T12:04:08Z")

</div>

2008/12/17 removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 17, 2008, 7:12pm UTC](https://www.ruby-forum.com/t/block-execution-of-script-while-same-script-is-already-running/155832/3 "2008-12-17T19:12:11Z")

</div>

On Wed, Dec 17, 2008 at 9:37 AM, removed\_email\_address@domain.invalid  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What is the best way to achieve this?
> 
> Thanks a lot.
> 
> –  
> Volker

Daniel B. 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
