473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Checking for duplicate instances of a script...

Hello, python beginner here,
How can I make sure that a given script is never run more than once at the
same time in a system ?
Besides checking 'ps' for its own name. Most modern langages have a direct
way to do that.
Thanks
--
Guillaume Dargaud
http://www.gdargaud.net/
"Here is your parachute and here is the manual.
Welcome to Linux."
Jul 18 '05 #1
5 1464

"Guillaume Dargaud" <NO**********@gdargaud.net> writes:
Hello, python beginner here,
How can I make sure that a given script is never run more than once at the
same time in a system ?


This can probably be done in *many* different ways, maybe there is
even an agreed upon on best solution. This is a small suggestion
based on fcntl.flock() (Unix only I am afraid):

#!/usr/bin/python
import struct, fcntl
import sys
lockfile = "/tmp/.lock"

## Open a file-descriptor for the lock file.
lockH = open(lockfile,"w")

## Try to aquire an exclusive lock on the lock file.
try:
fcntl.flock(lockH , fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError,e:
sys.exit("Lockfile %s already locked - an instance is probably already running" % lockfile)
##
## Your code ...
##
## Release the lock again
fcntl.flock(lockH, fcntl.LOCK_UN)
--
/--------------------------------------------------------------------\
/ Joakim Hove / ho**@bccs.no / (55 5) 84076 | \
| Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
| CMU | 5231 Paradis |
\ Thormøhlensgt.55, 5020 Bergen. | 55 91 28 18 /
\--------------------------------------------------------------------/
Jul 18 '05 #2
> This can probably be done in *many* different ways, maybe there is
even an agreed upon on best solution. This is a small suggestion
based on fcntl.flock() (Unix only I am afraid):


Wrong prediction: it works fine in Cygwin under windows, but it doesn't on
my linux server...

Traceback (innermost last):
File "./CounterTest.py", line 24, in ?
fcntl.flock(lockH, fcntl.LOCK_EX | fcntl.LOCK_NB)
TypeError: illegal argument type for built-in operation
Cygwin:
2.3.3 (#1, Dec 30 2003, 08:29:25)
[GCC 3.3.1 (cygming special)]

Linux:
1.5.2 (#1, Jan 31 2003, 10:58:35) [GCC 2.96 20000731 (Red Hat Linux 7.3 2

Is this too old ?

Maybe I'll try with os.open flags...
--
Guillaume Dargaud
http://www.gdargaud.net/
"My goal is to be a meteorologist. But since I possess no training in
meteorology, I suppose I should try stock brokerage." - Found in a
resume.
Jul 18 '05 #3
Guillaume Dargaud <NO**********@gdargaud.net> wrote:
Hello, python beginner here,
How can I make sure that a given script is never run more than once at the
same time in a system ?
Besides checking 'ps' for its own name. Most modern langages have a direct
way to do that.


1. Check 'ps'. But, if 2 processes checks at the same, then they see
nothing is running and decides to run themselves.

2. Use lockfile.

--
William Park, Open Geometry Consulting, <op**********@yahoo.ca>
Linux solution for data management and processing.
Jul 18 '05 #4
On Wed, 21 Jan 2004 10:28:20 +0100, "Guillaume Dargaud"
<NO**********@gdargaud.net> wrote:
Hello, python beginner here,
How can I make sure that a given script is never run more than once at the
same time in a system ?
Besides checking 'ps' for its own name. Most modern langages have a direct
way to do that.
Thanks

What modern language has a direct way to do that?
--dang
Jul 18 '05 #5
> >Hello, python beginner here,
How can I make sure that a given script is never run more than once at the
same time in a system ?
Besides checking 'ps' for its own name. Most modern langages have a direct
way to do that.
Thanks

What modern language has a direct way to do that?


You can register a COM or CORBA interface (is this what they are called?)
and attempt to connect to it during startup. If you can connect to
yourself, don't start up, if you can't, start up.

- Josiah

Jul 18 '05 #6

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

Similar topics

3
by: Giloosh | last post by:
Hello, i need some help if possible... i have a payments table with over 500 records i want to run a query that searches through the table spotting out any duplicate ID#'s and Dates. So basically...
13
by: Deepak Sarda | last post by:
Hello everyone. I have run into something which I believe is a bug or a shortcoming of the threading.Thread module. My program spawns 15 threads. For this I've creating a new class with...
7
by: Lowell Kirsh | last post by:
I have a script which I use to find all duplicates of files within a given directory and all its subdirectories. It seems like it's longer than it needs to be but I can't figure out how to shorten...
5
by: CJM | last post by:
In an application I'm working the user has the opportunity to record the despatching of one or more items with serial numbers. For each item they despatch, they have to chose the serial no that...
8
by: Kragen Sitaker | last post by:
ERROR: Cannot insert a duplicate key into unique index pg_class_relname_nsp_index We've been getting this error in our application every once in a while --- typically once an hour to once a day,...
1
by: Ennio-Sr | last post by:
Hi all! Testing a script where I need to make sure that postgresql is running before passing a <psql dbasename -c "insert into ..." > instruction I faced this curious behaviour: This is the...
0
by: Narendra Gc | last post by:
Hi... I am looking after a lan . I am i am not able to access one particular system , by another one particular system. But from these computers i can access all other systems in my lan and...
0
by: Gabriel Genellina | last post by:
En Fri, 18 Apr 2008 12:23:08 -0300, Shawn Milochik <Shawn@Milochik.comescribió: A dictionary with keys is perfectly reasonable. But a *list* of values has to be searched linearly for every...
7
by: john.cole | last post by:
I have searched all the groups I can, and I still haven't been able to come up the solution I need. I have the following problem. In my form named sbfrmSpoolList, I am entering a job, spool and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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
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...
0
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
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,...

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.