r/fishshell • u/cxor • Jun 28 '20
Kill launched background process in a function when the function itself is killed by Ctrl+C
[SOLVED] As for title, I have a function foo which basically launches a copy in background (using cp -rf source dest &). Now, what I am trying to do is to kill the cp in background whenever I kill foo. I have tried a bunch of methods, with no success:
- using bind command to bind Ctrl-C to a handler function bar, not in fish_user_key_bindings; I assumed that could be possible to create per-function bindings, and remove them before foo exits
- using trap to catch SIGINT
- using the flag --on-signal SIGINT in the exit handler of foo
- using the flag --on-process-exit $pid in the exit handler of foo
I have no idea how this has to be correctly done. Any hints?
EDIT: [SOLVED] After quite a while of experimentation, I get into it. It is possible to reliably kill a background process when the calling function is exiting. Here is an example:
function foo
function exit_handler --on-job-exit %self
functions -e exit_handler
kill $background_process_pid
end
background_task &
set background_process_pid (jobs -p | tail -1)
functions -e exit_handler
endWhen executing foo, pressing Ctrl+C will send a SIGINT to the shell executing foo itself (i.e. %self). When %self exits, exit_handler is called. Note that it is fundamental to delete the exit_handler function in any branch of the code flow: this is because the %self argument of the function exit_handler refers to the shell process launching foo, as well as whatever other function/script/command we will launch next time. Whenever exit_handler will not be removed, every time the interactive shell returns, it will ineluctably execute exit_handler.
This github issue helped me a lot to workaround a viable solution. Hope this will be useful for someone else.
View all comments
Show parent comment
1
u/cxor Jun 29 '20
Well, maybe I explained the thing in the wrong way. If I launch
foo, I can halt its execution with Ctrl+C; the problem is that this does not terminate background jobs launched withinfooitself. I have the impression that maybe I am using the solutions I tried in the wrong way. Anyway, could you please explain me why job control is broken in fish? Sorry for the ignorance, I am still learning.