In my bash script, I have a function that does one certain task in an infinite loop. To allow for the next part of my code to run at the same time, I run it with
How can I kill this function, when the rest of my program is done?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
There are several options. If this is your only background process then you can easily kill it with
You can save the PID of the subshell:
myFunc & myFunc_PID=$! [...] kill -HUP "$myFunc_PID"
You can also use
It makes sense to put the subshell killing in the EXIT handler:
trap 'kill %myFunc' EXIT