zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Rudi C <rudiwillalwaysloveyou@gmail.com>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: Weird behavior with the time command and background jobs
Date: Thu, 13 Aug 2020 11:15:53 -0700	[thread overview]
Message-ID: <CAH+w=7ZropggnbbsTd=GdWFvg-XasyVYwcRbwtun3=_dDFTVnQ@mail.gmail.com> (raw)
In-Reply-To: <CAE9z9A3X9CBRwxigtsQsGnC8J_CtpcKqz7FK0qXsHuJN5Kz8Bw@mail.gmail.com>

On Wed, Aug 12, 2020 at 8:34 AM Rudi C <rudiwillalwaysloveyou@gmail.com> wrote:
>
> ❯ f2() { eval "$@" & }

There's no subshell inside f2.

> ❯ time (f2 sleep 3 )
> ( f2 sleep 3; )  0.00s user 0.01s system 0% cpu 3.014 total

"time" forces its command to run synchronously, so here the subshell
you created at the command line waits for and reports the times of its
children, which includes the sleep even though the sleep itself is in
the background.

> ❯ f3() { (eval "$@" &) }

You have another subshell inside f3, so the eval'd command is a child
of that subshell, and a "grandchild" of the shell that runs f3.

> ❯ time (f3 sleep 3)
> ( f3 sleep 3; )  0.00s user 0.00s system 41% cpu 0.007 total

So here, the subshell inside f3 is not timed and not forced to be
synchronous, so it puts sleep in the background and immediately exits.
The subshell created at the command line, which is calculating the
times, only reports the time of the subshell inside f3, not of sleep.
A process can't time its grandchildren, only its children.

> Why is `f2` getting 3 seconds? It's running the job in the background, just like `f3`. In fact, their interactive behavior (without the time command) is the same.

As noted above, the time command changes the behavior of the outermost
subshell.  It has to do this in order to read the time statistics from
the operating system.


      reply	other threads:[~2020-08-13 18:17 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-12 15:18 Rudi C
2020-08-13 18:15 ` Bart Schaefer [this message]

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=7ZropggnbbsTd=GdWFvg-XasyVYwcRbwtun3=_dDFTVnQ@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=rudiwillalwaysloveyou@gmail.com \
    --cc=zsh-users@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).