zsh-users
 help / color / mirror / Atom feed
* Weird behavior with the time command and background jobs
@ 2020-08-12 15:18 Rudi C
  2020-08-13 18:15 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Rudi C @ 2020-08-12 15:18 UTC (permalink / raw)
  To: zsh-users

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

```
~
❯ f2() { eval "$@" & }

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

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

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

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.

[-- Attachment #2: Type: text/html, Size: 559 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Weird behavior with the time command and background jobs
  2020-08-12 15:18 Weird behavior with the time command and background jobs Rudi C
@ 2020-08-13 18:15 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2020-08-13 18:15 UTC (permalink / raw)
  To: Rudi C; +Cc: Zsh Users

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.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-08-13 18:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-12 15:18 Weird behavior with the time command and background jobs Rudi C
2020-08-13 18:15 ` Bart Schaefer

zsh-users

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/zsh-users

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-users zsh-users/ http://inbox.vuxu.org/zsh-users \
		zsh-users@zsh.org
	public-inbox-index zsh-users

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


code repositories for the project(s) associated with this inbox:

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

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git