473,480 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Allowing only one instance of a script?

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
4 2866
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2435
by: Leif K-Brooks | last post by:
I'm writing a site with mod_python which will have, among other things, forums. I want to allow users to use some HTML (<em>, <strong>, <p>, etc.) on the forums, but I don't want to allow bad...
8
3348
by: mytfein | last post by:
Hi Everyone, Background: Another department intends to ftp a .txt file from the mainframe, for me to process. The objective is to write a vb script that would be scheduled to run daily to...
3
11266
by: traceable1 | last post by:
Is there a way I can set up a SQL script to run when the instance starts up? SQL Server 2005 SP2 thanks!
4
1310
by: cj | last post by:
I've often used methods exposed by software purchased for other uses to automate tasks. For instance back in the late 90's I found the terminal emulation package the company exposed methods that...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7060
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6760
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7022
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3013
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3004
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.