Execute ninja build with background jobs

Viewed 73

I would like to start a background process (i.e. a job) from a Ninja build command : I want the command to start the job and return (leaving the job executing in background).
I am unable to do so: the Ninja build command does not return, as if it was waiting for the job to terminate (?).

Here is my environment:

  • foo.sh: a Bash script that starts a job printing "-> foo" every second
    bar() {
      while true; do
        echo "-> foo"
        sleep 1
      done
    }
    bar & disown
    echo "bar() running in PID=${!}"
    
  • build.ninja: the Ninja configuration, with a foo target that calls the foo.sh script
    rule CUSTOM_COMMAND
      command = $COMMAND
    
    build foo: CUSTOM_COMMAND
      COMMAND = bash ./foo.sh
      pool = console
    

When executing foo.sh directly from the terminal, it behaves as expected:

$ sh -c 'bash ./foo.sh'
-> foo
bar() running in PID=269795
$ # foo.sh has returned, the prompt is usable
-> foo
$
-> foo
-> foo
$ # a Ctrl-C has no action
$ kill 269795
$ # job killed, no more "-> foo" printing
$
  • The sh -c 'bash ./foo.sh' is used (rather than simply bash ./foo.sh) to be as close as possible to the Ninja processing:

"The Ninja command variable is passed directly to sh -c"

(source: https://ninja-build.org/manual.html)

But when executing the foo Ninja build, it does not return:

$ ninja foo
[0/1] bash ./foo.sh
-> foo
bar() running in PID=275837
-> foo
-> foo # command has not returned, the prompt is not usable
-> foo
-> foo
^Cninja: build stopped: interrupted by user. # a Ctrl-C stopped the execution
$ # process 275837 gets killed automatically

Why is ninja command not returning ?
How could I adapt foo.sh or build.ninja to reach the expected behaviour ?


Appendix:

It looks like ninja has a "zoombie" child process:

$ ps -aefxo pid,tty,ppid,pgid,sid,comm:100
...
 657811 pts/17    235551  657811  235551          \_ ninja
 657812 pts/17    657811  657811  235551              \_ sh <defunct>

This might be the same problem as issue #2052 (?).

0 Answers
Related