help-bash
[Top][All Lists]
Advanced

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

Re: assignment from background process


From: Greg Wooledge
Subject: Re: assignment from background process
Date: Fri, 24 Jan 2020 09:36:52 -0500
User-agent: Mutt/1.10.1 (2018-07-13)

On Fri, Jan 24, 2020 at 12:08:39PM +0000, address@hidden wrote:
> 
> payload=$(mosquitto_sub -h mqtthost.grumpy-net -p 1883 -i you -t foo/bar) &
> pid=$!

That doesn't work.  You can't launch a background process and expect it
to dump its information into a shell variable in the foreground parent.

Think about it for a moment, and you should see why.  Command substitution
works by establishing a pipe() between the parent and child processes,
with the parent waiting for the child to finish writing its output and
close the file descriptor (typically by exiting).

So, either the command substitution occurs between a child subshell
and the forked mosquito grandchild, and the value is only visible in the
child, not in the parent; or, the command substitution occurs between
the parent shell and the mosquito child, but the parent shell would have
to wait for the child to finish before it can say "OK, I have the
value, and now I can carry on".

There isn't any way to set up an asynchronous connection between an
external process and a shell variable, such that the shell variable
is magically updated "some time in the future" whenever the external
process happens to finish.

What you need is to decide upon, and implement, some form of interprocess
communication between the mosquito process and the shell script, so
that the shell script can collect the mosquito process's output whenever
it determines that the mosquito process is finished writing it.

The most likely way to do that will be to have the mosquito process
write to a plain old regular file.  Then, the shell script can read
the data out of that file.

That leaves the other half: the shell script needs to know *when* it
can read that file.  For this, the best arrangement is for the mosquito
process to be a direct child of the shell.  Then, you can either set
up a trap on SIGCHLD, which will be activated when any child finishes
(and then you'll need to check whether the mosquito process is still
running, because you won't know *which* child triggerd it); or, you
can do some form of polling, checking on the mosquito process every
once in a while as you do whatever else your script does; or,
depending on the exact nature of the mosquito program, perhaps it
uses atomic writes, or writes followed by an atomic rename, and
you can simply check for the existence, or the non-emptiness, of
the output file.

The choice is yours.



reply via email to

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