I’ve got two commands foo and bar.
foo runs for a long time without stdin or stdout/stderr activity. bar is a client of foo and runs with stdout/stderr but no stdin activity.
I’d like to run them from one shell, being able to kill both with ctrl-c, and to see the output from bar as it occurs.
i.e. something like this sequence
foo & bar kill -9
but without having to manually do the kill – instead it just happens on ctrl-c
is there a way of scripting this?
thanks
Don’t use
kill -9.You want to trap on
EXIT, not onINT.This solution will always make sure to terminate
fooandbar, whatever the reason for it exiting (excluding it beingSIGKILL‘ed).If you want to avoid keeping PIDs (which has some race condition issues) you can do this instead:
That’s a better (and cleaner!) solution if your script has no other jobs.
Ps: These solutions mean
fooandbarcan write to your terminal (your script’sstdout), but neither can read fromstdin. If you need eitherfooorbarto read from stdin, the solution becomes a bit more complex.