Send the current function to the background in bash

As the title says, I want to send the current function to the background. Something like

function myfunc {
    some_command | while read line
    #some_command must be running indefinitely
    do
        #if some condition on ${line} satisfies
        #Do something and go to background
    done
}

      

Is it possible?

I know it is possible to call a function directly and run it directly in the background. It's pretty obvious. This is not duplicated by this .

+3


source to share


1 answer


A simple command creates a process in the background and waits for it to complete, in your case the caller should continue when the condition is met asynchronously with the called call that continues to run.

This can be done with kill -STOP (the caller locks themselves) and kill -CONT (callle unblock caller)



function myfunc {
    some_command | while read line
    #some_command must be running indefinitely
    do
        #if some condition on ${line} satisfies
        kill -CONT $$
    done & kill -STOP $$
}

      

+3


source







All Articles