help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: running in the background


From: Greg Wooledge
Subject: Re: running in the background
Date: Wed, 9 Dec 2020 11:37:10 -0500
User-agent: Mutt/1.10.1 (2018-07-13)

On Wed, Dec 09, 2020 at 10:29:27AM -0600, grumpy@mailfence.com wrote:
> i frequently use something like
> 
> variable="$(command1 | command2 | etc)"
> 
> i would like to do this but have it all run in the background
> enlightenment appreciated

Well, you can't do it that way.  The output won't be available until the
background process has terminated, which defeats the purpose of putting
it in the background.

If you want the output of a background job, the first thing you have to
know is: *when* are you supposed to get it?  A millisecond from now?  Ten
seconds?  An hour?

The second thing you need to know is: *how* are you going to retrieve
this output?  Where should the background job put it, so that you can go
there to retrieve it?  Normally one would use a file for this.  Have the
background job redirect its output to this file, and then when you've
fulfilled the temporal obligations of the previous paragraph, you can go
read it from that file.

The most common answer to the first question would be, "whenever the
background job has terminated", which works pretty well if you have a
means of determining that as you go about your other, foreground, work.
You can either poll the process (e.g. with kill -0) periodically as
you go through your main loop, or you can set up a trap for SIGCHLD
which should be delivered to you when your child has terminated.  The
signal method works best if you only ever have *one* child at a time.

Other answers are possible, but this is really not a trivial subject.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]