zsh-workers
 help / color / mirror / code / Atom feed
From: Dave Yost <Dave@Yost.com>
To: zsh-workers@zsh.org
Subject: shell function in the background?
Date: Wed, 25 Jun 2014 07:34:01 -0700	[thread overview]
Message-ID: <2C39FCD4-02E6-4957-B292-486065C24639@yost.com> (raw)

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


Can a shell function tell if it’s part of a pipeline running in the background? 

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.

Can’t see how to do it.

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

0 Wed 0:12:34 DaveBook yost /tmp
250 Z% function foo() {
print foo says pid is $$
sleep 2
}
0 Wed 0:12:56 DaveBook yost /tmp
251 Z% foo & ps xl -t ttys004 | sort -k 2
[1] 26183
foo says pid is 25935
  UID   PID  PPID CPU PRI NI      VSZ    RSS WCHAN  STAT   TT       TIME COMMAND
    0 25934   226   0 143  0  2503604   2316 -      Ss   s004    0:00.01 login -pf yost /bin/zsh
  502 25935 25934   0  31  0  2500064   2512 -      S    s004    0:00.14 -zsh
  502 26183 25935   0 163  5  2500064    588 -      SN   s004    0:00.00 -zsh
    0 26184 25935   0  45  0  2433268    644 -      R+   s004    0:00.00 ps xl -t ttys004
  502 26185 26183   0 1074014208  5  2432764    500 -      SN   s004    0:00.00 sleep 2
  502 26186 25935   0 1074104586  0  2432800    500 -      R+   s004    0:00.00 sort -k 2
0 Wed 0:13:00 DaveBook yost /tmp
252 Z% 
[1]  + 26183 done       foo
0 Wed 0:13:02 DaveBook yost /tmp
252 Z%  print pid is $$
pid is 25935
0 Wed 0:21:00 DaveBook yost /tmp
253 Z% 


I get the same thing even if I do this

function foo() {(
  print foo says pid is $$
  sleep 2
)}
foo &

or this

function foo() {
  (
    print foo says pid is $$
    sleep 2
  ) &
}
foo



             reply	other threads:[~2014-06-25 14:34 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-25 14:34 Dave Yost [this message]
2014-06-25 15:55 ` Bart Schaefer
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=2C39FCD4-02E6-4957-B292-486065C24639@yost.com \
    --to=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).