Block execution of script while same script is already running

Discussion in 'Ruby' started by MrBanabas@googlemail.com, Dec 17, 2008.

  1. Guest

    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
     
    , Dec 17, 2008
    #1
    1. Advertising

  2. 2008/12/17 <>:
    > 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
     
    Robert Klemme, Dec 17, 2008
    #2
    1. Advertising

  3. On Wed, Dec 17, 2008 at 9:37 AM,
    <> 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
     
    Sean O'Halpin, Dec 17, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Benjamin Rutt
    Replies:
    2
    Views:
    943
    Peter Hansen
    Dec 20, 2005
  2. morrell
    Replies:
    1
    Views:
    964
    roy axenov
    Oct 10, 2006
  3. ashjas
    Replies:
    7
    Views:
    342
    ashjas
    Dec 10, 2007
  4. Strato
    Replies:
    6
    Views:
    334
  5. Christopher Brewster
    Replies:
    5
    Views:
    346
    John Machin
    Nov 14, 2008
Loading...

Share This Page