How to tell if ruby app is already running

Hi,

I have a FXRuby app, which is fine and dandy and does exactly what it
should. However, if the user, for whatever reason, decides to start two
instances of the app, bad things start happening.
My question: is there any way for my app to check if another instance of
itself is running before launching itself?

Thanks very much in advance.

···

--
Posted via http://www.ruby-forum.com/.

To make sure no more than 1 instance of the script is running:

gem install posixlock
require 'posixlock'
DATA.posixlock( File::LOCK_EX | File::LOCK_NB )

For full discussion:

http://tinyurl.com/2dsg5a9

Regards,

Dan

···

On 12/4/10 2:47 PM, Jim Burgess wrote:

Hi,

I have a FXRuby app, which is fine and dandy and does exactly what it
should. However, if the user, for whatever reason, decides to start two
instances of the app, bad things start happening.
My question: is there any way for my app to check if another instance of
itself is running before launching itself?

Hi,

I have a FXRuby app, which is fine and dandy and does exactly what it
should. However, if the user, for whatever reason, decides to start two
instances of the app, bad things start happening.
My question: is there any way for my app to check if another instance of
itself is running before launching itself?

Thanks very much in advance.

this will work in *nix and winblows:

···

On Dec 4, 2:47 pm, Jim Burgess <jack.ze...@gmail.com> wrote:

--
Posted viahttp://www.ruby-forum.com/.

This smells like a pure UNIX solution to me (and I don't know if Ruby
can deal with the Windows POSIX subsystem, as that is something that
isn't part of the default install on Windows).

What I would do is create a file in the app's startup code (in the
temporary directories of the OSes you target, for example, since that
directory gets cleaned rather often by the OS itself), and check if
this file exists on start up.

Of course, you get a problem if your application is killed by a task
manager (so, you could write the PID of your current process into the
lock file, and check if this PID exists before you terminate your app
prematurely).

···

On Sun, Dec 5, 2010 at 12:19 AM, Daniel Berger <djberg96@gmail.com> wrote:

On 12/4/10 2:47 PM, Jim Burgess wrote:

Hi,

I have a FXRuby app, which is fine and dandy and does exactly what it
should. However, if the user, for whatever reason, decides to start two
instances of the app, bad things start happening.
My question: is there any way for my app to check if another instance of
itself is running before launching itself?

To make sure no more than 1 instance of the script is running:

gem install posixlock
require 'posixlock'
DATA.posixlock( File::LOCK_EX | File::LOCK_NB )

--
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.

Hi,

Thanks very much for the replies.

I tried to install the posixlock gem, but got the following error
message:

C:/Ruby187/bin/ruby.exe extconf.rb
creating Makefile

make
gcc -I. -I/C/Ruby187/lib/ruby/1.8/i386-mingw32
-I/C/Ruby187/lib/ruby/1.8/i386-mingw32 -I. -g -O2 -DFD_SETSIZE=256
-c posixlock.c
posixlock.c:2:26: missing/file.h: No such file or directory
posixlock.c: In function `posixlock':
posixlock.c:82: error: storage size of 'lock' isn't known
posixlock.c:87: error: `F_RDLCK' undeclared (first use in this function)
posixlock.c:87: error: (Each undeclared identifier is reported only once
posixlock.c:87: error: for each function it appears in.)
posixlock.c:90: error: `F_WRLCK' undeclared (first use in this function)
posixlock.c:93: error: `F_UNLCK' undeclared (first use in this function)
posixlock.c:101: error: `F_SETLK' undeclared (first use in this
function)
posixlock.c:101: error: `F_SETLKW' undeclared (first use in this
function)
posixlock.c: In function `rb_file_lockf':
posixlock.c:160: error: storage size of 'lock' isn't known
posixlock.c:183: error: `F_WRLCK' undeclared (first use in this
function)
posixlock.c:184: error: `F_SETLKW' undeclared (first use in this
function)
posixlock.c:187: error: `F_RDLCK' undeclared (first use in this
function)
posixlock.c:192: error: `F_SETLK' undeclared (first use in this
function)
posixlock.c:199: error: `F_UNLCK' undeclared (first use in this
function)
posixlock.c:205: error: `F_GETLK' undeclared (first use in this
function)
make.exe: *** [posixlock.o] Error 1

I spent ages googling for a solution, but came up empty handed.

I then tried implementing Phillip's suggestion and that achieved exactly
what I had hoped.

Thanks again.

···

--
Posted via http://www.ruby-forum.com/.

To avoid file based locking you could just choose an application specific port and attempt to bind to it at startup.

Sam

···

On 10/12/10 05:33, ara.t.howard wrote:

On Dec 4, 2:47 pm, Jim Burgess<jack.ze...@gmail.com> wrote:
   

Hi,

I have a FXRuby app, which is fine and dandy and does exactly what it
should. However, if the user, for whatever reason, decides to start two
instances of the app, bad things start happening.
My question: is there any way for my app to check if another instance of
itself is running before launching itself?

Thanks very much in advance.

this will work in *nix and winblows:

a.rb · GitHub

--
Posted viahttp://www.ruby-forum.com/.
     

:slight_smile:

Dan

···

On 12/9/10 9:33 AM, ara.t.howard wrote:

On Dec 4, 2:47 pm, Jim Burgess<jack.ze...@gmail.com> wrote:

Hi,

I have a FXRuby app, which is fine and dandy and does exactly what it
should. However, if the user, for whatever reason, decides to start two
instances of the app, bad things start happening.
My question: is there any way for my app to check if another instance of
itself is running before launching itself?

Thanks very much in advance.

this will work in *nix and winblows:

a.rb · GitHub

Hi,

Thanks very much for the replies.

I tried to install the posixlock gem, but got the following error
message:

C:/Ruby187/bin/ruby.exe extconf.rb
creating Makefile

make
gcc -I. -I/C/Ruby187/lib/ruby/1.8/i386-mingw32
-I/C/Ruby187/lib/ruby/1.8/i386-mingw32 -I. -g -O2 -DFD_SETSIZE=256
-c posixlock.c
posixlock.c:2:26: missing/file.h: No such file or directory
posixlock.c: In function `posixlock':
posixlock.c:82: error: storage size of 'lock' isn't known
posixlock.c:87: error: `F_RDLCK' undeclared (first use in this function)
posixlock.c:87: error: (Each undeclared identifier is reported only once
posixlock.c:87: error: for each function it appears in.)
posixlock.c:90: error: `F_WRLCK' undeclared (first use in this function)
posixlock.c:93: error: `F_UNLCK' undeclared (first use in this function)
posixlock.c:101: error: `F_SETLK' undeclared (first use in this
function)
posixlock.c:101: error: `F_SETLKW' undeclared (first use in this
function)
posixlock.c: In function `rb_file_lockf':
posixlock.c:160: error: storage size of 'lock' isn't known
posixlock.c:183: error: `F_WRLCK' undeclared (first use in this
function)
posixlock.c:184: error: `F_SETLKW' undeclared (first use in this
function)
posixlock.c:187: error: `F_RDLCK' undeclared (first use in this
function)
posixlock.c:192: error: `F_SETLK' undeclared (first use in this
function)
posixlock.c:199: error: `F_UNLCK' undeclared (first use in this
function)
posixlock.c:205: error: `F_GETLK' undeclared (first use in this
function)
make.exe: *** [posixlock.o] Error 1

I spent ages googling for a solution, but came up empty handed.

I did not realize you were on Windows. I think it could be made to
work on Windows, though. But, you can still use this even on Windows
without resorting to a 3rd party gem, as per the link to the original
discussion:

DATA.flock(File::LOCK_EX)

I then tried implementing Phillip's suggestion and that achieved exactly
what I had hoped.

gem install pidfile :slight_smile:

Regards,

Dan

···

On Dec 5, 5:13 am, Jim Burgess <jack.ze...@gmail.com> wrote: