in reply to system >> 8 is non-zero when child exits with exit(0)

... if I find an answer I'll report back here.

The answer was that in some cases the scripts were being run manually by people, and often this would be done by:

  1. running the scripts
  2. sending them into the background
  3. the people logging back out again at which point STDOUT and STDERR were closed.

When the child process then tried to write to STDOUT/STDERR the non-zero exit code would result.

I've not fully unravelled this thread to the root cause - but it is something that does not happen in older debian releases (though when the change happened I'm not sure).

The solution is either to avoid writing to closed STDOUT/STDERRs (but that's fiddly/fragile across a lot of code) or better and simpler to redirect the output of processes that are going to be backgrounded using something like '>/dev/null 2>&1' or nohup which also redirects STDOUT/STDERR.

If the output is needed for some time then probably the easiest approach is to launch the process in a terminal manager like screen/tmux for the duration of the run.