4

As the title says, I want to send a running 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 that it is possible to call a function directly & to directly run it in the background. That is pretty obvious. This is not duplicate of this.

Raghuram Vadapalli
  • 1,190
  • 2
  • 13
  • 27
  • Possible duplicate of [executing shell command in background from script](https://stackoverflow.com/questions/3683910/executing-shell-command-in-background-from-script) – Aserre Jul 24 '17 at 15:19
  • 1
    Why are you trying to do this? You can call your function form a separated bash script an call this with an ampersand. Then your function is run in the background. – schorsch312 Jul 24 '17 at 15:21
  • Why not `while true; do if ...; then do_something & break; fi` – William Pursell Jul 24 '17 at 15:31
  • @WilliamPursell I edited the question. Now it should be clear why I am asking. – Raghuram Vadapalli Jul 24 '17 at 15:39
  • I don't know if you can send a function to the background. But you surely can send a process or the script to the background. You just need to send an SIGSTOP signal. – Aditya Jul 24 '17 at 15:45
  • @Aditya SIGSTOP sends it to background and stops. I need it to be running in background. – Raghuram Vadapalli Jul 24 '17 at 15:46
  • This really doesn't make any sense. The net effect is that `myfunc` runs in the background, but the caller waits until the first time 'some condition' is satisfied. To accomplish that, have the caller block on a read from a pipe, then run the function in the background and have it write to that pipe when the condition is met. – William Pursell Jul 24 '17 at 15:47
  • @WilliamPursell Can you explain it clearly in an answer. The condition is on ${line} if that is not clear. – Raghuram Vadapalli Jul 24 '17 at 15:50
  • What is it you want in the background? It's not at all clear why you don't just invoke the function in the background initially, or why you can't just do `if condition; then do_something & fi` and do something in the background. – William Pursell Jul 24 '17 at 15:58

1 Answers1

4

A simple command creates a process in background and waits for its termination, in your case the caller must continue when a condition is met asynchronously with callee which continues running.

This can be done with kill -STOP (caller blocks himself) and kill -CONT (callee 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 $$
}
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36
  • 1
    FYI, the `function` keyword makes this code needlessly noncompliant with the POSIX sh specification -- it would be portable to a wider array of shells if the first line were `myfunc() {`, with the `function` keyword removed and the `()` syntax added. – Charles Duffy Jul 24 '17 at 17:27