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

I will show two different "classes" of ways, both involves 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](http://stackoverflow.com/questions/185451/quick-and-dirty-way-to-ensure-only-one-instance-of-a-shell-script-is-running-at) and [here](http://stackoverflow.com/questions/6482602/bash-script-run-only-once).

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 succeeded, will hold the lock on the file until that instance of the program exists. If the 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](http://stackoverflow.com/questions/1444790/python-module-for-creating-pid-based-lockfile).

The one answer on that question that uses [zc.lockfile](https://pypi.python.org/pypi/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=...