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

M

Mike

Does python support named semaphores for Linux? I saw that ActivePython
for Win32 does.

Can I get a list of currently running processes?

I already posted in the GTK forum about looking for the window title.

Thx
 
D

David M. Wilson

Mike said:
Can I get a list of currently running processes?

There is no portable way of doing it, however if you have a POSIX
environment, you can parse the output of ps:

def get_process_ids():
output = []
ps = os.popen('ps -o pid,command -C python')
ps.readline()

for line in ps:
bits = line.lstrip()[:-1].split(' ')
output.append( (int(bits[0]), ' '.join(bits[1:])) )

return output


Don't hold me to this - I tested it on BSD and Linux, and as far as I
know both the options I have used are portable.


David.
 
M

Mike

Mike said:
Can I get a list of currently running processes?

There is no portable way of doing it, however if you have a POSIX
environment, you can parse the output of ps:

def get_process_ids():
output = []
ps = os.popen('ps -o pid,command -C python')
ps.readline()

for line in ps:
bits = line.lstrip()[:-1].split(' ')
output.append( (int(bits[0]), ' '.join(bits[1:])) )

return output


Don't hold me to this - I tested it on BSD and Linux, and as far as I
know both the options I have used are portable.


David.
Excellant. Thank you!
 
M

Miki Tebeka

Hello Mike,

You can have your program create a known directory (which IIRC an
atomic operation). When a new instance tries to create it, there will
be an exception.

Make sure that when you application is closing to remove this
directory.

HTH.
Miki
 
J

Jarek Zgoda

Miki Tebeka said:
You can have your program create a known directory (which IIRC an
atomic operation). When a new instance tries to create it, there will
be an exception.

Make sure that when you application is closing to remove this
directory.

Traditional way is to create pidfile somewhere -- in user's home
directory, /var/run...
 
B

Ben Finney

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.
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top