[Tutor] how to know if process is running?

Alan Gauld alan.gauld at btinternet.com
Tue Jul 28 20:00:25 CEST 2009


"shawn bright" <shawn at skrite.net> wrote 

> So, my question is, how can i write something that will know if there
> is an instance of that something already running?
> 
> I am doing this in python 2.5 on Ubuntu, if that matters ( which i
> suspect it does ).

I suspect the simplest way is to use the ps command and grep.
Wrap your code in a shell script that checks ps before launching 
the script.

If thats not acceptable call ps from subprocess module as soon 
as you start, hopefuilly that won;t break anything although the 
processes will be running together briefly.
If so  you could do the check then fork() or exec() to create the 
real new process...

Finally you can write a signal file into /tmp and then look for that 
file on startup. Don't forget to delete it when you exit though!

Lots of options depending on how hard core you need to be!

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list