skarnet / execline

The execline scripting language
https://skarnet.org/software/execline/
ISC License
149 stars 19 forks source link

Wait on any process #10

Closed computerquip-work closed 1 year ago

computerquip-work commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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.

skarnet commented 2 years ago

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.

computerquip-work commented 2 years ago

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.

skarnet commented 2 years ago

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 commented 1 year ago

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

Closing this.