Determining if program executing in shell background?

sik0fewl xxdigitalhellxx at hotmail.com
Fri Mar 28 17:12:03 EST 2003


Kevin Perrott wrote:
> I'm VERY new to python programming so please be gentle.
> 
> I'm trying to find a function that will tell me if a pyton program is
> executing in the background when launched using the:
> 
> <program name> &
> 
> shell syntax.
> 
> I'm trying to utilize the curses module to output messages to 'windows' and
> the system seems to stop the program when I try to get it to run in the
> background.  I'm assuming it has something to do with the curses module
> requiring control of a proper stdout or something along those lines.
> 
> I wrote a small test program using the isatty() function:
> 
> #!/usr/bin/python
> import sys
> import os
> isatty = os.isatty(sys.stdout.fileno())
> if isatty == 1 :
>   print("isatty")
> else :
>   print("isnotatty")
> 
> that I thought would do the trick but "isatty" always prints whether
> launched by
> 
> <program>
> or
> <program> &
> 
> I must be missing something somewhere and would appreciate any help you
> guys/gals could give me.


If it's not a problem you could run the program having stdout and stderr 
pipe to /dev/null and put to background:

program.py 2>&1 >/dev/null &

or have a -D switch that changes sys.stdout and sys.stderr to a custom 
class that does nothing.

I'm honestly not sure if there is a better way to do this, but if this 
is being run automatically, instead of by the user either of the above 
solutions should be fine.

-- 
Ryan





More information about the Python-list mailing list