Determining if program executing in shell background?

Grant Edwards grante at visi.com
Fri Mar 28 17:00:55 EST 2003


In article <Bd3ha.11$de.8820 at news2.telusplanet.net>, Kevin Perrott wrote:

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

That's correct.  Either way, stdout is connected to the
terminal where the program was started.

> I must be missing something somewhere and would appreciate any
> help you guys/gals could give me.

I'm guessing that the program is trying to query stdin for a
character or status and has been stopped since stdin is not
available.  I'm not really sure how to programmatically check
for that condition.

You may want to add a command-line option to your program to
tell it that it's to run in "daemon" mode so that it won't try
to use ncurses.

If you really want to use ncurses when run in the background,
you should ask about it in a group where Thomas Dickey is
likely to see the question.  Try
comp.os.linux.development.apps, and make sure that ncurses is
in the subject line.

-- 
Grant Edwards                   grante             Yow!  I KAISER ROLL?! What
                                  at               good is a Kaiser Roll
                               visi.com            without a little COLE SLAW
                                                   on the SIDE?




More information about the Python-list mailing list