[comp.unix.questions] How does a shell know if it's in the foreground or the background?

pjs@euclid.jpl.nasa.gov (Peter Scott) (04/25/91)

One of our users asked this and I'm stumped.  You have a script and
you want to know whether the user is sitting there at the terminal
waiting with baited breath for it to complete or whether the hyperactive
creature typed an "&" after it and went on with other work.  I thought
I could just test $prompt, but that only tells me whether I've
source'd a script or not.  How can a csh script know whether it's on
the job list?  

-- 
This is news.  This is your       |    Peter Scott, NASA/JPL/Caltech
brain on news.  Any questions?    |    (pjs@euclid.jpl.nasa.gov)

subbarao@phoenix.Princeton.EDU (Kartik Subbarao) (04/25/91)

In article <1991Apr24.232959.6247@elroy.jpl.nasa.gov> pjs@euclid.jpl.nasa.gov writes:
>One of our users asked this and I'm stumped.  You have a script and
>you want to know whether the user is sitting there at the terminal
>waiting with baited breath for it to complete or whether the hyperactive
>creature typed an "&" after it and went on with other work.  I thought
>I could just test $prompt, but that only tells me whether I've
>source'd a script or not.  How can a csh script know whether it's on
>the job list?  

In shells that support job control (like csh), jobs are put into different 
process group than the parent shell to facilitate job control. The process 
group of the controlling tty is that of the current (foreground) job. What
your shellscript can do is to check whether the script's pgrp (or pid, for
that matter, since it's a group leader) matches the pgrp of the tty. If
they match, then the job is in the foreground. Otherwise, it's a background
job. Here is some sample code that does this:

/* pgrp.c */

# include <stdio.h>
main()
{
	printf("%d\n", tcgetpgrp(0); 
}

Compile this program, and call it within your shellscript:

#!/bin/csh 

if (`pgrp` == $$) then
	echo foreground
else
	echo background
endif


				-Kartik

--
internet# rm `df | tail +2 | awk '{ printf "%s/quotas\n",$6}'`

subbarao@phoenix.Princeton.EDU -| Internet
kartik@silvertone.Princeton.EDU (NeXT mail)  
SUBBARAO@PUCC.BITNET			          - Bitnet

lewis@tramp.Colorado.EDU (LEWIS WILLIAM M JR) (04/26/91)

In the Bourne shell AND if not more than one background job is running

	if [ "$$" -eq "$!" ]

will be true if the job is in the background.

guy@auspex.auspex.com (Guy Harris) (04/27/91)

>In shells that support job control (like csh), jobs are put into different 
>process group than the parent shell to facilitate job control.

Note also that, in shells that support job control, "foreground" and
"background" are temporary conditions, not ways of life; a job could be
in the foreground one minute, and in the background a bit later, and
then back in the foreground again a bit later....

guy@auspex.auspex.com (Guy Harris) (04/28/91)

>In the Bourne shell AND if not more than one background job is running
>
>	if [ "$$" -eq "$!" ]
>
>will be true if the job is in the background.

Is that even true with the System V Release 4 Bourne shell, if it's
invoked as "jsh" to turn job control on?  What happens if the job
started out in the background and was moved into the foreground?

(Once again: "with job control, 'background' and 'foreground' are
temporary conditions, not states of mind."  Does your program or script
have to deal with the case of somebody moving it between foreground and
background?  If so, *does* it deal with it?)

rvp@softserver.canberra.edu.au (Rey Paulo) (05/03/91)

In article <7454@auspex.auspex.com> guy@auspex.auspex.com (Guy Harris) writes:
>>In shells that support job control (like csh), jobs are put into different 
>>process group than the parent shell to facilitate job control.
>
>Note also that, in shells that support job control, "foreground" and
>"background" are temporary conditions, not ways of life; a job could be
>in the foreground one minute, and in the background a bit later, and
>then back in the foreground again a bit later....

How do you do this?  Suppose I have a program which I started as a background
process.  After a definite period of time, I want the program to run in the
foreground without my intervention.  After doing some job in the foreground,
I want the program to sit back in the background and repeat the cycle. Note,
that shifting from background to foregorund and background is all done by
the program itself.  How do I do this?
-- 
Rey V. Paulo                  | Internet:  rvp@csc.canberra.edu.au 
University of Canberra        | I am not bound to please thee with my answer. 
AUSTRALIA                     |         -Shylock, in "The Merchant of Venice" 
------------------------------+----------------------------------------------