473,813 Members | 3,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

testing if another instance of a script is already running

Hi folks,

I want to write some kind of test to check at startup if another
instance of my script is already running.

I don't want to handle writing of a PID file because it is too
Unix/Linux specific way to do this, and I need to keep the code to be
cross-platform.

I think the better way to achieve this is to use some process control,
but I'm a neebie and I don't see how to do this in a safe and clean way.

Any idea ?

Best regards,
Strato
Sep 12 '08 #1
6 6392
Strato wrote:
Hi folks,

I want to write some kind of test to check at startup if another
instance of my script is already running.

I don't want to handle writing of a PID file because it is too
Unix/Linux specific way to do this, and I need to keep the code to be
cross-platform.

I think the better way to achieve this is to use some process control,
but I'm a neebie and I don't see how to do this in a safe and clean way.

Any idea ?

Best regards,
Strato
Here is a recipe for Linux version of singleinstance class:

http://code.activestate.com/recipes/546512/

and I wrote and contributed the equivalent Windows version:

http://code.activestate.com/recipes/474070/

Hope this helps.

-Larry
Sep 13 '08 #2
Strato wrote:
Hi folks,

I want to write some kind of test to check at startup if another
instance of my script is already running.

I don't want to handle writing of a PID file because it is too
Unix/Linux specific way to do this, and I need to keep the code to be
cross-platform.

I think the better way to achieve this is to use some process control,
but I'm a neebie and I don't see how to do this in a safe and clean way.

Any idea ?

Best regards,
Strato
Here is a recipe for Windows version of singleinstance class:

http://code.activestate.com/recipes/474070/

and I wrote and contributed the equivalent Linux version:

http://code.activestate.com/recipes/546512/

Hope this helps.

-Larry
Sep 13 '08 #3
On Sep 12, 7:08*am, Strato <pyt...@p0w.org wrote:
Hi folks,

I want to write some kind of test to check at startup if another
instance of my script is already running.

I don't want to handle writing of a PID file because it is too
Unix/Linux specific way to do this, and I need to keep the code to be
cross-platform.

I think the better way to achieve this is to use some process control,
but I'm a neebie and I don't see how to do this in a safe and clean way.

Any idea ?

Best regards,
Strato
You could use msvcrt.locking, and just lock the script file. I am not
sure about this.
Sep 13 '08 #4
>I don't want to handle writing of a PID file because it is too
Unix/Linux specific way to do this, and I need to keep the code to be
cross-platform.

I think the better way to achieve this is to use some process
control, but I'm a neebie and I don't see how to do this in a safe
and clean way.
AaronYou could use msvcrt.locking, and just lock the script file. I
Aaronam not sure about this.

If you want a cross-platform solution, you might try the lockfile module
instead:

http://pypi.python.org/pypi/lockfile

Skip
Sep 13 '08 #5
On Sep 13, 5:34*pm, s...@pobox.com wrote:
* * >I don't want to handle writing of a PID file because it is too
* * >Unix/Linux specific way to do this, and I need to keep the code to be
* * >cross-platform.
* * >>
* * >I think the better way to achieve this is to use some process
* * >control, but I'm a neebie and I don't see how to do this in a safe
* * >and clean way.

* * AaronYou could use msvcrt.locking, and just lock the script file. *I
* * Aaronam not sure about this.

If you want a cross-platform solution, you might try the lockfile module
instead:

* *http://pypi.python.org/pypi/lockfile

Skip
Would it suffice to call 'os.open' with flags= _O_CREAT| _O_EXCL ?
Would that be platform-independent?

Windows docs (_open):

_O_CREAT| _O_EXCL
Returns an error value if the file specified by filename exists.
Applies only when used with _O_CREAT.

man page:

O_EXCL
If O_EXCL and O_CREAT are set, open will fail if the file exists.
The check for the existence of the file and the creation of the file
if it does not exist is atomic with respect to other processes
executing open naming the same filename in the same directory with
O_EXCL and O_CREAT set.

CreateDirectory :

The CreateDirectory function does seem to guarantee this as well,
though is not as explicit:

Possible errors include the following.
ERROR_ALREADY_E XISTS
The specified directory already exists.

But the lock operation wouldn't need the 'if hasattr(os, "link")' test.
Sep 14 '08 #6

AaronWould it suffice to call 'os.open' with flags= _O_CREAT| _O_EXCL
Aaron? Would that be platform-independent?

I suspect it would be platform-independent but not NFS-safe. (The other
solutions in lockfile might have NFS problems as well. In any case,
lockfile provides a bit more functionality as well, including timeouts and
the ability to break locks.

Skip
Sep 14 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
10531
by: Pierre-Yves | last post by:
Hello, I would like to prevent my perl program to be executed several times simultaneously (if the program is already running, I would like to display a message like "another instance of this program is already running, please try again in a couple of minutes). For doing this, I guess I have to check the running processes... but I don't know how to do that and how I can identify my program in the running processes.
8
3043
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to what I really should do. I've had a look through Programming Python and the Python Cookbook, which have given me ideas, but nothing has gelled yet, so I thought I'd put the question to the community. But first, let me be a little more detailed...
1
1623
by: Kevin Otte | last post by:
Hi, I'm currently working on a program that is supposed to allow only one instance of itself. How can I transfer the data that is passed to a second instance as a parameter to the first instance. One part of my program's functionality is that it parses all links from a webpage. Well, a script catches them and starts my tool with the links-string as a parameter. Within my app I want to be able to select some of those links and store...
4
26830
by: CliffKing | last post by:
I am trying to use Windows Task Scheduler to run a batch file for an already open MS Access database. Below is the syntax to the batch file: Batch file: DailySalesExport.bat REM This runs the macro that exports the reports to the J drive PATH = "C:\Program Files\Microsoft Office\Office\;C:\Windows\Command" START /WAIT Msaccess.exe "D:\acesdata\SALES\Daily2005.mdb" /x "SnapShotExportDailySales" EXIT
25
2611
by: acemtp | last post by:
Hello, I would like to show you a new collaborative website for javascript programmer. It's quite hard to know if some specific code you wrote will work on all browsers, and if not, why it doesn't work. You often have to install lot of browsers on your computer, buy a mac or a pc, and make unit tests.
2
4307
by: pamela fluente | last post by:
I have an application running. A file type is registered with this application. When the user click on a file of such type a new instance of the application is loaded with command line (file name). I want to shut done this new instance an to notify the instance which is already running, by passing the command line to it (so that the file can be open by the application already runnning). Here is my code, how do I notify the running...
4
1943
by: David | last post by:
Hi list. Do test-driven development or behaviour-driven development advocate how to do higher-level testing than unit testing? types of testing: unit integration system
7
2201
by: alito | last post by:
Hi all, I am new to using packages to group my modules. I can't figure out how to run a module that uses relative imports without writing a wrapper that imports that module. Everything I try it complains that I am attempting a relative import in a non-package. eg ~/python/testpackage$ ls config.py importer.py __init__.py
0
1282
by: Tim Golden | last post by:
Tim Golden wrote: Sorry, not the best of links to point to. Basically, search mail.python.org for things like "CreateMutex" and "single application instance". TJG
0
9734
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9607
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10667
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10407
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10422
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5568
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3030
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.