Is there an elegant way, in the shell, to say “run this program until this other program is finished”?
I know I could run a thing, get a PID and then append a command with “&& kill pid” or something but that feels clunky.
Is there an elegant way, in the shell, to say “run this program until this other program is finished”?
I know I could run a thing, get a PID and then append a command with “&& kill pid” or something but that feels clunky.
@mhoye isn’t that what the ‘wait’ cmd does?
Edit: oh I understand what you’re saying. That sounds more complicated and now I also want to know if there’s an existing solution.
I don't see any reason not to just kill the screensaver progA after long.running.progB terminates. If progA were _doing_ something with files, then maybe send it a SIGTERM/HUP not a SIGKILL, but if its NOT doing i/o just kill it.
1. run and background progA&
2. progB
3. kill -1/15 pogA (or by PID)
You're just hung up (heh) over a hard kill feeling clunky? If progA is written correctly itll properly handle a SIGINT/TERM/HUP and close nice.
If you don't see any reason to do the thing I specifically said I wanted to do, consider that an opportunity for inquiry rather than sanctimony. With exactly one hundred percent of the respect this is due, if I wanted to just kill the process manually when I observe that it is finished, the question I'm asking would not exist.
@mhoye
This works by killing the whole subshell process tree:
`(run_this & other_program; kill 0)`
Tried with `(vlc & sleep 3s; kill 0)`
@mhoye Unfortunately, I think the Unix process and signal model makes this inextricably messy. There's no polite general way to ask a process to terminate, and you can only easily find out about a child ending if you're its parent. So you're going to have to kill the process with a signal and that means knowing its PID and ... mess.
(If the program has a specific API where you can say 'please exit', you can use it, but that's not a general solution.)
Is that not what SIGTERM is? (I suppose it could be argued that a signal that defaults to immediate termination isn't entirely "polite", but hopefully a program that would actually be sensitive to such impoliteness would install a handler for it to do appropriate things before exiting.)
you can only easily find out about a child ending if you're its parent.In terms of portable mechanisms yes, though on a reasonably recent Linux you can use poll(2) or the like on a a pidfd for a non-child process.
(Offhand I don't know of any existing tools that expose pidfds in a shell-convenient way, but if someone were sufficiently desperate I expect a bash extension loaded via enable -f could probably do it in a reasonably ergonomic fashion.)
@mhoye uh I’m away from my desk and this is probably wrong but can you run a sub shell like
{ myexpensivecommand; kill $PPID } &