I have this line:
trap 'jobs -p | xargs kill -9' SIGINT SIGTERM EXIT
it is repeated in many bash shell scripts I have.
What is the best way to share this code? Can I call a bash function perhaps?
In reality, I am creating a framework and the user will be expected to write some glue shell scripts. What would be nice if the user's shell scripts could inherit from a base shell script, somehow. Or they could just call bash functions that pre-exist somehow.
The problem is that if I create a bash function like so:
// a.sh function trap_and_kill_child_jobs { trap 'jobs -p | xargs kill -9' SIGINT SIGTERM EXIT }
and call it from another script like so:
// b.sh source ./a.sh trap_and_kill_child_jobs sh -c 'sleep 10000 &' & # I want this process to be killed by `trap_and_kill_child_jobs` ./run-some-tests.js
the caller script (b.sh
) does not actually experience the trap. The 'child jobs' created by b.sh will continue running.
trap_and_kill_child_jobs
function wouldn't invoke trap to actually work?node
works. I'm not a JS developer, but I expect what's going on is thatnode
is forking and starting some other processes, and then exiting. So it won't show up in your "jobs" list at all. Again, a shell scripting framework is almost invariably code smell. You have architectural problems here; you're trying to use the shell to keep track of internal workings of JS when it really can't do that.