bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Checking executability for asynchronous commands


From: Greg Wooledge
Subject: Re: Checking executability for asynchronous commands
Date: Mon, 28 Dec 2020 08:15:27 -0500
User-agent: Mutt/1.10.1 (2018-07-13)

On Sun, Dec 27, 2020 at 08:02:49AM -0500, Eli Schwartz wrote:
> I'm not sure I understand the question?

My interpretation is that for an async *simple* command of the
form   cmd args &   where cmd cannot be executed at all (due to
lack of permissions, perhaps, or because the external program is
not installed), they want bash to set $? to a nonzero value to
indicate that the command couldn't even be started.

I've seen similar requests several times over the years.

The problem is that the parent bash (the script) doesn't know, and
cannot know, that the command was stillborn.  Only the child bash
process can know this, and by the time this information has become
available, the parent bash process has already moved on.

The only way the parent can obtain this information is to wait until
that information becomes available.  The obvious problem here is that
the parent does not know when that information will become available.
So, one is stuck choosing from among the following strategies:

1) After launching the async command, sleep for some fraction of a
   second, and then check whether the child is still running.  If
   it isn't running, retrieve its exit status.

2) Set up a SIGCHLD handler (trap), and process the child's exit status
   whenever the trap fires.

3) Poll "kill -0" on the child's PID during the script's main loop.

Each of these strategies has its advantages and flaws.  None of them
is correct for every script.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]