How to determine if an instance of your program is already running?

Ben Finney bignose-hates-spam at and-benfinney-does-too.id.au
Sun Dec 28 03:26:28 EST 2003


On Sun, 28 Dec 2003 08:00:33 +0000 (UTC), Jarek Zgoda wrote:
> Traditional way [to avoid multiple instances] is to create pidfile
> somewhere -- in user's home directory, /var/run...

More precisely, the "pidfile" is a file named "processname.pid", which
contains a single line of text: the process ID (number) of the running
process.  /var/run/processname.pid is the usual place.  When the program
is requested to exit, it deletes the file while cleaning up.

On startup the program (or some simple wrapper script) checks the
existence of this file; if it exists, it reads the PID contained in the
file and checks the status of the process.  It then quits immediately if
the process is already running.

What to do in the case that the PID file exists, but the process isn't
running, is up to you to decide; possibly you can warn the user that a
prior instance of the application didn't exit properly.

-- 
 \       "Anytime I see something screech across a room and latch onto |
  `\    someone's neck, and the guy screams and tries to get it off, I |
_o__)      have to laugh, because what is that thing?"  -- Jack Handey |
Ben Finney <http://bignose.squidly.org/>




More information about the Python-list mailing list