Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: shell function in the background?

On Jun 25, 2014 9:34 AM, "Dave Yost" <Dave@xxxxxxxx> wrote:
> Can a shell function tell if it’s part of a pipeline running in the

That's a tricky question because all parts of a pipeline run "in the
background" except for the last (first, in most shells other than zsh)
command.  So you're really asking whether the function can tell if some
other process downstream of it has been put in the background.  There's no
direct way to do that.

> I want to write a shell function that traps SIGCONT and does one thing or
another thing depending on whether the function is CONTinuing in the

I'm not sure that would work for you anyway, because the timing of delivery
of the signal to your function and to other jobs in the pipeline is not
deterministic.  Also, because of vagaries of job control and memory
management, the shell trap handler for CONT might not be called immediately.

Is there some other condition related to being in the background that you
might test instead?  I.e. why does backgrounding require different behavior?

> Furthermore, it’s not clear to me why a backgrounded function thinks its
pid is the pid of the shell that spawned it.

That's the way the $$ variable is defined by the standard and is how all
Unix shells have always behaved.

If you "zmodload zsh/system" the parameter $pid becomes available and has
the value you want.

Messages sorted by: Reverse Date, Date, Thread, Author