zsh-users
 help / color / Atom feed
* is '&' the right way to do this?
@ 2019-12-17 12:33 TJ Luoma
  2019-12-18  4:38 ` dana
  0 siblings, 1 reply; 2+ messages in thread
From: TJ Luoma @ 2019-12-17 12:33 UTC (permalink / raw)
  To: Zsh MailingList

Every now and again I realize that I've developed a habit, but it
isn't the "right" or "best" way to do something, so I'm asking what
may be an obvious question.

If I am in a zsh shell script (not interactive) and want to have it do
something tangential which does not block the progress of the
main/parent script, then I usually put it in (parentheses) with a & at
the end of the line

So, to use a dramatic example:

#!/bin/zsh -f

echo "This is the start"

( find / -type f -print > /tmp/filelist.txt ) &

echo "This is the end"

exit 0

My intention is that the script would exit long before `find` was done
writing to  '/tmp/filelist.txt'.

Question #1:  Would it be better for any reason to use `&|` such as:

#!/bin/zsh -f

echo "This is the start"

( find / -type f -print > /tmp/filelist.txt ) &|

echo "This is the end"

exit 0

or is the use of `&|` really only beneficial for interactive shells?


Question #2: Are there better ways of doing this?


Thanks!

TjL

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

* Re: is '&' the right way to do this?
  2019-12-17 12:33 is '&' the right way to do this? TJ Luoma
@ 2019-12-18  4:38 ` dana
  0 siblings, 0 replies; 2+ messages in thread
From: dana @ 2019-12-18  4:38 UTC (permalink / raw)
  To: TJ Luoma; +Cc: Zsh MailingList

On 17 Dec 2019, at 06:33, TJ Luoma <luomat@gmail.com> wrote:
> ( find / -type f -print > /tmp/filelist.txt ) &

Technically you don't need the parentheses in this example

On 17 Dec 2019, at 06:33, TJ Luoma <luomat@gmail.com> wrote:
> Question #1:  Would it be better for any reason to use `&|` such as:
> or is the use of `&|` really only beneficial for interactive shells?

When the MONITOR and HUP options are both enabled, the shell will
automatically send SIGHUP to all jobs in the job-control table when it shuts
down (which usually kills their processes). Disowning a background job with
&|, &!, or disown removes it from the job-control table, which prevents it
from receiving the signal

Since MONITOR is only enabled by default in interactive shells, the main
benefit of disowning doesn't usually apply in scripts. However, it doesn't
hurt anything either, and it may be useful in rare cases, e.g., where you
specifically want to exclude certain jobs from the job-control table

On 17 Dec 2019, at 06:33, TJ Luoma <luomat@gmail.com> wrote:
> Question #2: Are there better ways of doing this?

There are other options that you might use in some scenarios, like coproc and
nohup and setsid and even systemd, but & is probably fine for the general use
case of 'run a job asynchronously / in the background'. I think so, anyway

dana


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

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-17 12:33 is '&' the right way to do this? TJ Luoma
2019-12-18  4:38 ` dana

zsh-users

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

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


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