By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

Allowing only one instance of a script?

P: n/a
Ali
Hi,

I have a script which I double-click to run. If i double-click it
again, it will launch another instance of the script.

Is there a way to allow only one instance of a script, so that if
another instance of the script is launched, it will just return with an
error.

Thanks

Regards,
Ali

Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Am Wed, 22 Jun 2005 23:49:21 -0700 schrieb Ali:
Hi,

I have a script which I double-click to run. If i double-click it
again, it will launch another instance of the script.

Is there a way to allow only one instance of a script, so that if
another instance of the script is launched, it will just return with an
error.


Hi,

Create a file which contains the PID (process ID) of
the current process in a directory. If the file
already exists, the file is running.

If your script dies without removing the pid-file, you
need to look during the start if the PID which is in the
file is sill alive.

There is a small race condition between os.path.exists()
and writing the file. If you want to be 100% sure you need
to use file locking.

HTH,
Thomas
--
Thomas GŁttler, http://www.thomas-guettler.de/
Jul 19 '05 #2

P: n/a
On 2005-06-23, Thomas Guettler <gu*****@thomas-guettler.de> wrote:
Create a file which contains the PID (process ID) of
the current process in a directory. If the file
already exists, the file is running.
That's how it's usually done.
If your script dies without removing the pid-file, you need to
look during the start if the PID which is in the file is sill
alive.
There is a small race condition between os.path.exists()
and writing the file.
That's why it's pointless to call os.path.exists().
If you want to be 100% sure you need to use file locking.


I've never seen it done that way.

The standard method is to use open() with flags O_CREAT|O_EXCL.
If the open() is sucessful, then you have the lock. If it
fails, somebody else already has the lock.

Another method is to create a temp file containing the PID and
then call link() to rename it.

Both open() and link() are atomic operations, so there's no
race condition.

--
Grant Edwards grante Yow! I don't know WHY I
at said that... I think it
visi.com came from the FILLINGS inmy
read molars...
Jul 19 '05 #3

P: n/a
In article <11*************@corp.supernews.com>,
Grant Edwards <gr****@visi.com> wrote:

Both open() and link() are atomic operations, so there's no
race condition.


....unless you're running under NFS.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

f u cn rd ths, u cn gt a gd jb n nx prgrmmng.
Jul 19 '05 #4

P: n/a
On 2005-06-23, Aahz <aa**@pythoncraft.com> wrote:
In article <11*************@corp.supernews.com>,
Grant Edwards <gr****@visi.com> wrote:

Both open() and link() are atomic operations, so there's no
race condition.


...unless you're running under NFS.


I think read somewhere than NFS 3 handles the open with
CREAT+EXCL in an atomic manner (older versions didn't). I don't
know about link.

--
Grant Edwards grante Yow! Will it improve my
at CASH FLOW?
visi.com
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.