You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've recently moved to fish and I'm missing the 'wait' command. Is there any particular reason why it is not implemented? Or maybe it exists, but i've somehow overlooked it?
It seems for me it's not implementable outside of the shell since it must have acces to the jobs list. Perhaps it should be a function...
I wrote an implementation using fg, it almost the same but slightly different output.
functionwait-d"Wait for the specified process and report its termination status. If N is not given, all currently active child processes are waited for, and the return code is zero. N may be a process ID or a job specification; if a job spec is given, all processes in the job's pipeline are waited for."# The above help text is taken from `help wait` in a bash shell on Mac OS X.iftest-n"$argv"fg$argvreturn$statusendfor pid in (jobs-p|tail +1)
ifkill-0$pid2>/dev/null
fg$pidendendreturn$statusend
It seems to me that a built-in is a reasonable offering here. The absence of the wait command makes fish less expressive than other shells. maxnordlund's solution is clever, but it is inelegant to ask everyone to use that.
I too think this should be implemented. Copying the interface from traditional shells like bash seems like an obvious starting point. It's not obvious to me as a relatively new user of the fish shell that there is any need to or merit in deviating from that interface.
Well, I thought that perhaps I'd just write the built-in myself and offer a patch (or a pull request or whatever the protocol is) but I looked into bash's implementation and realized that there is a non-trivial aspect to a good implementation of wait: You need to hold onto job exit status until either the jobs have been waited for (or, as an optimization, until it can be determined that the job identity is no longer knowable to the shell user). POSIX only requires that the last CHILD_MAX such job statuses are retained.
I believe that it's worth considering an altered or extended interface that is capable of unambiguously supplying exit status information. See wait4(2). (In POSIX shells, an exit status greater than 127 is indistinguishable from an exit due to receipt of a signal.) It's also worth considering supplying a means to request exit status information for all the processes in a pipeline. That said, a POSIX-esque implementation would be entirely adequate for my needs.
retain-exit-statusname
Retain under the name name a handle to exit status/waiting for the last job started or placed in the background.
(Equivalently, as far as I am concerned: retain-exit-status
Retain a handle to exit status/waiting for the last job started or placed in the background. Return a name for this handle.)
wait-on-jobsname...
Arguments are names of handles created by retain-exit-status. wait-on-jobs waits until at least one of the jobs associated with the supplied names has exited and returns the names for such jobs. If at least one of the named jobs had already exited, wait-on-jobs does not block; otherwise, it blocks only until one job has exited.
extract-exit-statusname
Return exit status information for the job associated with name, which must have previously been returned by wait-on-jobs.
The retain-exit-status command makes the contract for when the shell must retain exit status information explicit. The wait-on-jobs command makes it easy to wait on an arbitrary set of jobs. The extract-exit-status command provides an interface for returning exit information, divorced of the complications surrounding when to block, and gives you a clean point at which to end the shell's responsibility for the exit status information.
I omitted as many details as I at first thought I could but actually...
tl;dr Let the user be explicit about when he may subsequently ask to wait on a job or know its exit status, and make him able to wait on arbitrary sets of jobs.
@roselma: I think at first we should try to figure out what functionality is needed.
I've never really used wait so I can only comment from the outside, but for me it seems that you'll want to:
Suspend execution until a certain process or job finishes
Read the exit status for that process
The main question I've had is: How do we deal with multiple jobs, especially regarding return code? wait 42 666 should obviously (tm) only exit when both 42 and 666 have exited, but when 42 fails (and then returns "54") but 666 succeeds, should we communicate that fact, and how?
There's a couple solutions I can come up with:
Save status to a different (perhaps user-chosen) variable, probably as a list - this is basically what you've proposed with retain-exit-status.
Only return 0 when all jobs have been successful and 1 if at least one failed (or the number of failed jobs), and perhaps additionally output return codes to stdout
Make $status a list - this would break test $status -eq 0, though if it's only used in a new function... (still, inconsistency is bad)
And now for some healthy self-criticism: I said above that wait 42 666 should "obviously" only exit if both exited. Should it? Am I stupid? Evil? (Yes I am 🎸). Of course that matches POSIX wait, but there might be a case where you'd wait for one. There might be a case for a "--any" option.
is a kind of boilerplate, and it's not explicit about whether you may want to wait on the command or not. The retain-exit-status idea is nice and explicit. We could roll it into the background syntax, taking a cue from redirections:
sleep 3 &handle
Now handle would be the name of the job for use in other commands. Or if we don't like changing this syntax, there's #1381 which suggests having a command decoration (like exec) to mean background.
One advantage of this is that even retain-exit-status is in a sense annoyingly late, since the backgrounded job may finish even before the retain-exit-status job is executed. So we still have to speculatively hold onto job return values for at least a period.
Regarding return codes, assuming wait 42 666 returns only after both have exited, it's not a big deal to return the exit code of the first failed process, or 0 if everyone succeeded. If you want to get individual exit codes, you can issue two separate wait commands.
If you want to get individual exit codes, you can issue two separate wait commands.
Going by the same logic as above, you can't - the second job could have finished in the meantime. Unless of course we save the status and job id for at least a while - though if someone ever exceeds that while, they'd be in for a nasty surprise, and holding them indefinitely means using memory for potentially a long time (I've heard there are people who go months without rebooting or restarting the shell - never quite got that myself). That is, of course, unless we save it in a scoped variable that can be garbage collected.
Unless of course we save the status and job id for at least a while
Any wait will put us on the hook for doing this at least some of the time, since a backgrounded process may finish even before the next command executes.
Other shells have a problem: they don't know if you're going to wait or not, so they have to speculatively hold onto exit statuses, potentially forever. The idea for fixing this is to indicate at job creation time whether it will be waited for in the future. If you don't indicate that you're going to wait, we can discard the exit status immediately.
A script that promises to wait but does not would create a (puny) leak, but then that would be a script bug that we could fix. It's much better than relying on heuristics and introspection like POSIX suggests.
If this wait command is to be non-POSIX, please do not make the semantics of it be such that it does not return until all jobs specified have exited.
If it returns after at least one job has exited, it would be trivial to implement the wait-for-all semantics in a shell function. Conversely, it is impossible to implement wait-for-any semantics.
That is not a good default behaviour, hide the wait-for-any behind a flag (or just use a spinner like @bucaran did). Most of the time, if not all of the time, I use wait is to create a synchronization barrier for set of background jobs. Kinda like xargs except the jobs can heterogeneous, e.g. update brew/vim/pip at the same time, then wait for all them to complete.
That would spin, not block. I don't know about your application, but in my case, expending one core worth of CPU cycles to wait for process termination is not a viable alternative.
It's interesting that fish's fg returns zero when there are no background jobs. I would call that a bug of its own.
Activity
zanchey commentedon Apr 22, 2014
It's not implemented, but it might be a useful addtion; a naïve implementation should be easy to do as a function (with
sleepandpsor similar).ibqn commentedon Jun 1, 2014
indeed, I am also missing it.
maxnordlund commentedon Sep 24, 2014
I wrote an implementation using
fg, it almost the same but slightly different output.zanchey commentedon Sep 29, 2014
The problem with using
fgis that Ctrl-C will kill the target program, not the wait utility.roselma commentedon Jan 5, 2016
It seems to me that a built-in is a reasonable offering here. The absence of the wait command makes fish less expressive than other shells. maxnordlund's solution is clever, but it is inelegant to ask everyone to use that.
(Just a +1.)
ridiculousfish commentedon Jan 6, 2016
Yes,
waitseems natural to me. Do you (or anyone) want to propose what its interface should be?krader1961 commentedon Jan 6, 2016
I too think this should be implemented. Copying the interface from traditional shells like bash seems like an obvious starting point. It's not obvious to me as a relatively new user of the fish shell that there is any need to or merit in deviating from that interface.
maxnordlund commentedon Jan 6, 2016
Yeah, I would need/want just waiting for all jobs without argument and waiting for specific jobs when given job ids and/or pids of those jobs.
It should probably do nothing if there are no jobs, maybe print an error message if it's run interactively.
roselma commentedon Jan 6, 2016
Well, I thought that perhaps I'd just write the built-in myself and offer a patch (or a pull request or whatever the protocol is) but I looked into bash's implementation and realized that there is a non-trivial aspect to a good implementation of wait: You need to hold onto job exit status until either the jobs have been waited for (or, as an optimization, until it can be determined that the job identity is no longer knowable to the shell user). POSIX only requires that the last CHILD_MAX such job statuses are retained.
I would strongly encourage not providing a wait command until/unless job status for already-exited but un-waited-for jobs can be supplied by the command; to do otherwise will enable people to unknowingly write buggy scripts, as is explained under "Application Usage" in the POSIX spec:
http://pubs.opengroup.org/onlinepubs/009695399/utilities/wait.html
I believe that it's worth considering an altered or extended interface that is capable of unambiguously supplying exit status information. See wait4(2). (In POSIX shells, an exit status greater than 127 is indistinguishable from an exit due to receipt of a signal.) It's also worth considering supplying a means to request exit status information for all the processes in a pipeline. That said, a POSIX-esque implementation would be entirely adequate for my needs.
ridiculousfish commentedon Jan 7, 2016
Really great find @roselma . I think that section makes a compelling case for redesigning the
waitinterface.roselma commentedon Jan 8, 2016
@ridiculousfish A proposal:
retain-exit-statusnameRetain under the name name a handle to exit status/waiting for the last job started or placed in the background.
(Equivalently, as far as I am concerned:
retain-exit-statusRetain a handle to exit status/waiting for the last job started or placed in the background. Return a name for this handle.)
wait-on-jobsname...Arguments are names of handles created by
retain-exit-status.wait-on-jobswaits until at least one of the jobs associated with the supplied names has exited and returns the names for such jobs. If at least one of the named jobs had already exited,wait-on-jobsdoes not block; otherwise, it blocks only until one job has exited.extract-exit-statusnameReturn exit status information for the job associated with name, which must have previously been returned by
wait-on-jobs.The
retain-exit-statuscommand makes the contract for when the shell must retain exit status information explicit. Thewait-on-jobscommand makes it easy to wait on an arbitrary set of jobs. Theextract-exit-statuscommand provides an interface for returning exit information, divorced of the complications surrounding when to block, and gives you a clean point at which to end the shell's responsibility for the exit status information.I omitted as many details as I at first thought I could but actually...
tl;dr Let the user be explicit about when he may subsequently ask to wait on a job or know its exit status, and make him able to wait on arbitrary sets of jobs.
faho commentedon Jan 8, 2016
@roselma: I think at first we should try to figure out what functionality is needed.
I've never really used
waitso I can only comment from the outside, but for me it seems that you'll want to:The main question I've had is: How do we deal with multiple jobs, especially regarding return code?
wait 42 666should obviously (tm) only exit when both 42 and 666 have exited, but when 42 fails (and then returns "54") but 666 succeeds, should we communicate that fact, and how?There's a couple solutions I can come up with:
retain-exit-status.test $status -eq 0, though if it's only used in a new function... (still, inconsistency is bad)And now for some healthy self-criticism: I said above that
wait 42 666should "obviously" only exit if both exited. Should it? Am I stupid? Evil? (Yes I am 🎸). Of course that matches POSIX wait, but there might be a case where you'd wait for one. There might be a case for a "--any" option.ridiculousfish commentedon Jan 8, 2016
The current two-step:
is a kind of boilerplate, and it's not explicit about whether you may want to
waiton the command or not. Theretain-exit-statusidea is nice and explicit. We could roll it into the background syntax, taking a cue from redirections:Now
handlewould be the name of the job for use in other commands. Or if we don't like changing this syntax, there's #1381 which suggests having a command decoration (likeexec) to mean background.One advantage of this is that even
retain-exit-statusis in a sense annoyingly late, since the backgrounded job may finish even before theretain-exit-statusjob is executed. So we still have to speculatively hold onto job return values for at least a period.Regarding return codes, assuming
wait 42 666returns only after both have exited, it's not a big deal to return the exit code of the first failed process, or 0 if everyone succeeded. If you want to get individual exit codes, you can issue two separate wait commands.faho commentedon Jan 8, 2016
Going by the same logic as above, you can't - the second job could have finished in the meantime. Unless of course we save the status and job id for at least a while - though if someone ever exceeds that while, they'd be in for a nasty surprise, and holding them indefinitely means using memory for potentially a long time (I've heard there are people who go months without rebooting or restarting the shell - never quite got that myself). That is, of course, unless we save it in a scoped variable that can be garbage collected.
ridiculousfish commentedon Jan 8, 2016
Any
waitwill put us on the hook for doing this at least some of the time, since a backgrounded process may finish even before the next command executes.Other shells have a problem: they don't know if you're going to wait or not, so they have to speculatively hold onto exit statuses, potentially forever. The idea for fixing this is to indicate at job creation time whether it will be waited for in the future. If you don't indicate that you're going to wait, we can discard the exit status immediately.
A script that promises to wait but does not would create a (puny) leak, but then that would be a script bug that we could fix. It's much better than relying on heuristics and introspection like POSIX suggests.
faho commentedon Jan 8, 2016
What would
wait SOMEIDthen do if you didn't specify that? Always return 0? Return 0 if the job still existed?Or maybe the "command decoration" approach is the right one, with scoped handles, i.e.:
faho commentedon Jan 8, 2016
Also, I love that label!
ridiculousfish commentedon Jan 8, 2016
wait SOMEIDcould be either an error or a no-op if the handle isn't valid.roselma commentedon Jan 9, 2016
If this wait command is to be non-POSIX, please do not make the semantics of it be such that it does not return until all jobs specified have exited.
If it returns after at least one job has exited, it would be trivial to implement the wait-for-all semantics in a shell function. Conversely, it is impossible to implement wait-for-any semantics.
maxnordlund commentedon Jan 9, 2016
That is not a good default behaviour, hide the wait-for-any behind a flag (or just use a spinner like @bucaran did). Most of the time, if not all of the time, I use wait is to create a synchronization barrier for set of background jobs. Kinda like
xargsexcept the jobs can heterogeneous, e.g. update brew/vim/pip at the same time, then wait for all them to complete.maxnordlund commentedon Jan 26, 2016
I realized this simply synchronization command can be written like this:
roselma commentedon Jan 27, 2016
That would spin, not block. I don't know about your application, but in my case, expending one core worth of CPU cycles to wait for process termination is not a viable alternative.
It's interesting that fish's fg returns zero when there are no background jobs. I would call that a bug of its own.