Determining if program executing in shell background?

Donn Cave donn at u.washington.edu
Fri Mar 28 18:37:02 EST 2003


Quoth "Kevin Perrott" <kevin at riversidehonda.com>:
| 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.

You might try this:

	if os.tcgetpgrp(0) == os.getpid(0):
		print 'foreground'
	else:
		print 'background'

This is basically asking if the current process is the process leader
of the foreground process group for tty device unit 0 (standard input.)

	Donn Cave, donn at u.washington.edu




More information about the Python-list mailing list