execline icon indicating copy to clipboard operation
execline copied to clipboard

Wait on any process

Open computerquip-work opened this issue 3 years ago • 5 comments

Bash has job control functionality that lets you provide a set of pids and then exit when any one of them exits. I think this can be implemented with wait and using signal(SIGCHLD, SIG_IGN). As far as I know, since it's a builtin in bash and there's no other command to do this, currently if you have multiple background processes and need to wait on any of them to die, it's not possible with just execline.

computerquip-work avatar Apr 12 '22 22:04 computerquip-work

After a bit of research, there's already a waitn implementation internally. forx and gang use that which I then realized allows the -p argument which means I can run commands in parallel... which means I can already do this.

computerquip-work avatar Apr 12 '22 23:04 computerquip-work

I'm going to open this back up since I couldn't get this working. I'm not sure if this is an intended use or not.

Basically, all I'm doing is this:

background {
    sleep 10
}

importas -i -u PID1 !

background {
    sleep 5
}

importas -i -u PID2 !

forx -p -x 0 CUR_PID {
    $PID1
    $PID2
}
importas -ui CUR_PID CUR_PID
wait { $CUR_PID }

I can't seem to figure out why this doesn't work. I'm assuming it's something to do with the variable substitution in wait { $CUR_PID } but if it expands to an empty word it should wait for all children and if it doesn't expand at all, I would expect an error. Instead, it just exits immediately.

computerquip-work avatar Apr 13 '22 17:04 computerquip-work

wait can only wait for processes that are its direct children. The fact that you're interposing a forx process prevents it from working.

Replace the whole forx statement with just wait { $PID1 $PID2 } and it should do what you want.

skarnet avatar Apr 13 '22 20:04 skarnet

The problem I'm having with wait { $PID1 $PID2 } is that I want to exit if either $PID1 exits or $PID2 exits, not when both exit.

computerquip-work avatar Apr 13 '22 20:04 computerquip-work

Oh, that's right. It's an interesting feature to have; I will add it in a future version. In the meantime, indeed, I don't think there's a simple way to do this in execline at the moment, sorry.

skarnet avatar Apr 13 '22 21:04 skarnet

The wait command now has a -o option that does exactly what you want.

Closing this.

skarnet avatar Nov 03 '22 18:11 skarnet