Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to determine if an instance of your program is already running?

Reply
Thread Tools

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

 
 
Mike
Guest
Posts: n/a
 
      12-27-2003
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


 
Reply With Quote
 
 
 
 
David M. Wilson
Guest
Posts: n/a
 
      12-27-2003
Mike <(E-Mail Removed)> wrote...

> 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.
 
Reply With Quote
 
 
 
 
Mike
Guest
Posts: n/a
 
      12-28-2003
On Sat, 27 Dec 2003 03:42:15 -0800, David M. Wilson wrote:

> Mike <(E-Mail Removed)> wrote...
>
>> 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!
 
Reply With Quote
 
Miki Tebeka
Guest
Posts: n/a
 
      12-28-2003
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
 
Reply With Quote
 
Jarek Zgoda
Guest
Posts: n/a
 
      12-28-2003
Miki Tebeka <(E-Mail Removed)> pisze:

> 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...

--
Jarek Zgoda
Unregistered Linux User #-1
http://www.zgoda.biz/ JID:zgoda-a-chrome.pl http://zgoda.jogger.pl/
 
Reply With Quote
 
Ben Finney
Guest
Posts: n/a
 
      12-28-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/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Creating an instance when the argument is already an instance. Olive Python 4 07-06-2012 02:01 PM
Can script determine if window.onload has already fired? Matt Kruse Javascript 55 12-01-2009 04:50 AM
How to determine the PID of your Java program andreww100@gmail.com Java 3 07-24-2005 10:25 PM
How to determine whether the user has already selected an option from a multi select list Patrick Olurotimi Ige ASP .Net 6 03-11-2005 10:39 AM



Advertisments