473,480 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
Jul 18 '05 #1
5 2986
Mike <Mi**@kordik.net> 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.
Jul 18 '05 #2
On Sat, 27 Dec 2003 03:42:15 -0800, David M. Wilson wrote:
Mike <Mi**@kordik.net> 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!
Jul 18 '05 #3
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
Jul 18 '05 #4
Miki Tebeka <mi*********@zoran.com> 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/
Jul 18 '05 #5
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/>
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3819
by: Mark Fisher | last post by:
I have a Java desktop GUI application that the user can run multiple times. In order to keep one instance of the application distinct from another, I'd like to put the instance number of the...
4
394
by: Koert | last post by:
Hi, is there a simple way in .Net to determine if there is already an instance running of an application ? I want to avoid that a user starts an application twice. Thanks, Koert
7
6898
by: Ross Presser | last post by:
OK, I've been researching this problem and can't find a definitive answer yet. The situation is one that seems to have come up a few times to different folks. I am writing an application that...
18
5635
by: Steve Barnett | last post by:
I want to ensure that there is only ever one instance of my app running on a single PC at any time. I understand that I can achieve this by using a mutex and, if I can't take ownership of the...
7
1439
by: Sam | last post by:
Hi, I know how to allow only one instance of my .net application per user using mutex. Now I want to warn the user if someone else is using the application on another pc of the same network. How...
6
5504
by: perera | last post by:
I want to create a MFC program *.exe. My reguirement is at a time only one instance can be run. In Other on mutile instnces can be run at a time
3
7179
by: Mark Jerde | last post by:
VS 2005. When I google "CSharp single instance form" the returned pages usually use a Mutex or the VB.NET runtime library....
20
10591
by: Boki Digtal | last post by:
Hi All, How to limit the c# program can only run one instance at the same time ? Thank you! Best regards, Boki.
10
43457
by: John Brown | last post by:
Hi there, Does anyone know how to (generically) determine the currently active form for an application using a "static" function (so I can call it from anywhere). There is no offiical way I've...
0
7059
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6758
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7010
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4499
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3011
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.