I want to prevent starting the program if it is already running. Right now the same user can start the program multiple times and cause unexpected behavior.

Is this something I should solve at the linux level (e.g. chmod something) or at the python level (if a special file in a special place exists, exit, otherwise create that special file in that special place). This option just seems a little kluge.


1 Answer 1


There are some different ways of achieving this. All of them depend on at least some provision in your code. There is no way of doing that exclusively in the Operating System level.

I will show two different "classes" of ways, both involve creating entries on the file system.

Python Only Lockfile

You can find two different ways of doing that in a bash script (conceptually the same) here and here. Can can use the same idea in a Python code, the bash shell script language is just for demonstrating the concept.

The idea is to work with a file on the file system that is created when the process starts, and a way of determining whether, when the process starts and find such file on the file system, if the other process that created it is still alive.

Of course you should remember to remove that file on exit, if possible (if not terminated by a -9 or something).

OS File Locking

In this approach your code will request a file lock from the Operating System, and, if succeeds, will hold the lock on the file until that instance of the program exits. If instance B is unable to achieve a lock, that means that one other instance A has the lock, so your instance B should exit.

You can find examples of that approach in Python here.

The one answer on that question that uses zc.lockfile seems the most adequate to me.

From zc.lockfile's manual:

The ZODB lock_file module provides support for creating file system locks. These are locks that are implemented with lock files and OS-provided locking facilities. To create a lock, instantiate a LockFile object with a file name:

>>> import zc.lockfile
>>> lock = zc.lockfile.LockFile('lock')

If we try to lock the same name, we'll get a lock error:

>>> import zope.testing.loggingsupport
>>> handler = zope.testing.loggingsupport.InstalledHandler('zc.lockfile')
>>> try:
...     zc.lockfile.LockFile('lock')
... except zc.lockfile.LockError:
...     print("Can't lock file")
Can't lock file
>>> for record in handler.records: # doctest: +ELLIPSIS
...     print(record.levelname+' '+record.getMessage())
ERROR Error locking file lock; pid=...
  • I'd prefer to not have to add a shell script on top of the python script. I like the zc.lockfile module, thanks!
    – tarabyte
    Jul 11, 2014 at 16:52
  • The shell script was just an example, so you can use the same concept in a Python source. I don't think shell scripts can hold locks, so that's probably the reason that all the examples of the first class I found were shell script.
    – Marco Poli
    Jul 11, 2014 at 16:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.