Checking for duplicate instances of a script...

G

Guillaume Dargaud

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
 
J

Joakim Hove

Guillaume Dargaud said:
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 / (e-mail address removed) / (55 5) 84076 | \
| Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
| CMU | 5231 Paradis |
\ Thormøhlensgt.55, 5020 Bergen. | 55 91 28 18 /
\--------------------------------------------------------------------/
 
G

Guillaume Dargaud

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.
 
W

William Park

Guillaume Dargaud said:
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.
 
D

Dang Griffith

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
 
J

Josiah Carlson

Hello, python beginner here,
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top