zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Dave Yost <Dave@yost.com>
Cc: zsh-workers@zsh.org
Subject: Re: shell function in the background?
Date: Wed, 25 Jun 2014 08:55:49 -0700	[thread overview]
Message-ID: <CAH+w=7YAyW0T4icPNF0Kci1TGiA5CoSjEiMxFmPqwh+Hykj=Zg@mail.gmail.com> (raw)
In-Reply-To: <2C39FCD4-02E6-4957-B292-486065C24639@yost.com>

[-- Attachment #1: Type: text/plain, Size: 1420 bytes --]

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

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
background.

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.

  reply	other threads:[~2014-06-25 15:56 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-25 14:34 Dave Yost
2014-06-25 15:55 ` Bart Schaefer [this message]
2014-06-25 17:56   ` Philippe Troin
2014-07-24 11:41   ` Peter Stephenson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7YAyW0T4icPNF0Kci1TGiA5CoSjEiMxFmPqwh+Hykj=Zg@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=Dave@yost.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).