Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Mike
Guest
 
Posts: n/a
#1: Jul 18 '05
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



David M. Wilson
Guest
 
Posts: n/a
#2: Jul 18 '05

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


Mike <Mike@kordik.net> wrote...
[color=blue]
> Can I get a list of currently running processes?[/color]

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.
Mike
Guest
 
Posts: n/a
#3: Jul 18 '05

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


On Sat, 27 Dec 2003 03:42:15 -0800, David M. Wilson wrote:
[color=blue]
> Mike <Mike@kordik.net> wrote...
>[color=green]
>> Can I get a list of currently running processes?[/color]
>
> 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.[/color]
Excellant. Thank you!
Miki Tebeka
Guest
 
Posts: n/a
#4: Jul 18 '05

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


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
Jarek Zgoda
Guest
 
Posts: n/a
#5: Jul 18 '05

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


Miki Tebeka <miki.tebeka@zoran.com> pisze:
[color=blue]
> 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.[/color]

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

--
Jarek Zgoda
Unregistered Linux User #-1
http://www.zgoda.biz/ JID:zgoda-a-chrome.pl http://zgoda.jogger.pl/
Ben Finney
Guest
 
Posts: n/a
#6: Jul 18 '05

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


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

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/>
Closed Thread