[comp.unix.shell] getting exit status from background job in sh

rodgers@clausius.mmwb.ucsf.edu (06/05/91)

In <1991Jun3.180151.11210@dartvax.dartmouth.edu> mjm@eleazar.dartmouth.edu (Andy Behrens) writes:

>Thanks to Maarten Litmaath for this little gem.

The Litmaath script is indeed a gem; we have modified it a bit so that it
can, upon specification of an option, save the status in a file, which is
useful if the rsh command is placed in the background from within a script.
One can then carry on with other work, then use the wait command to ensure
that rsh is done, read the exit status from the file, and act appropriately.

So on to a related problem: if one puts a LOCALLY executed command into the
background from within a script, then later issues a wait command so as to be
able to do something with output from the command, how can one cause the exit
status for the background command to have been saved in a file? 

Cheerio, Rick Rodgers
R. P. C. Rodgers, M.D.         (415)476-2957 (work) 664-0560 (home)
UCSF Laurel Heights Campus     UUCP: ...ucbvax.berkeley.edu!cca.ucsf.edu!rodgers
3333 California St., Suite 102 Internet: rodgers@maxwell.mmwb.ucsf.edu
San Francisco CA 94118 USA     BITNET: rodgers@ucsfcca

maart@nat.vu.nl (Maarten Litmaath) (06/06/91)

In article <rodgers.676057135@clausius.mmwb.ucsf.edu>,
	rodgers@clausius.mmwb.ucsf.edu writes:

>[...] we have modified it a bit so that it
>can, upon specification of an option, save the status in a file, which is
>useful if the rsh command is placed in the background from within a script.
>One can then carry on with other work, then use the wait command to ensure
>that rsh is done, read the exit status from the file, and act appropriately.

You need not modify `ersh' to accomplish what you want.
This is how to do it in the Bourne shell:

	ersh machine cmd < input > output 2> errors &
	pid=$!		# remember the process ID
	# lots of other commands
	wait $pid	# wait for the specified process
	status=$?	# the exit status of the process is returned

If you _must_ use a file, use the following:

	(ersh machine cmd redirections; echo $? > status_file) &

In the csh you would have to resort to this:

	((ersh machine cmd < input > output) >& errors; \
		echo $status > status_file) &

>So on to a related problem: if one puts a LOCALLY executed command into the
>background from within a script, then later issues a wait command so as to be
>able to do something with output from the command, how can one cause the exit
>status for the background command to have been saved in a file? 

That question has been answered by now.