0

I am using a coprocess inside my main parent process to spawn commands to a shell that otherwise cannot be solved (the shell that I open in the coprocess is not maintained by me and executes the "newgrp" and "exec" commands that stop me from sending commands to that shell simply from my script... So I need the coprocess to be able to execute commands in that shell from a script). So far I have been using one thread, the parent process to push commands to the coprocess but now I am in need of spawning commands from several child processes, too, because of an optimization step. The bash doc says, file descriptors are not inherited by child processes, and this is in fact true, when I opened a subshell I got the following error message from bash:

[...]/automated_integration/clif_ai_common.sh: line 396: ${!clifAi_sendCmdToCoproc_varName}: Bad file descriptor

The code that makes this message appear is as follows:

if [[ ${PARAM_NO_MOVING_VERIF_TB_TAGS} != true ]]; then
    (
        clifAi_log ${CLIFAI_LOGLEVEL_INFO} "" "clifAi_sanityRegression_callbackRunning" "Populating moving VERIF and TB tags in the background..."

        clifAi_popVerifTags "${clifAi_sanityRegression_callbackRunning_coproc}" "${clifAi_sanityRegression_callbackRunning_wslogfile}" "${PARAM_OPTLEVEL}" "${CONST_EXCLUDE_FILTER}" "${CONST_DIR_TO_OPT}" ${clifAi_sanityRegression_callbackRunning_excludeList}
        clifAi_popTbTags "${clifAi_sanityRegression_callbackRunning_coproc}" "${clifAi_sanityRegression_callbackRunning_wslogfile}"

        rm -rf ${VAR_VERIFTBTAG_SEMAPHORE_FILE}
    ) &
fi

Bash reports the same error if I move this piece of code into a function and call it with & without the ( ), so no subshell. This is also understandable; it will still spawn a child process, regardless of running it in a subshell or not.

My question is, how can I write to the coprocess owned by the parent process from child processes, too? What is the best practice?

Many thanks in advance, Geza Balazs

  • Can you highlight the error line (396). The code that is listed does not include reference to clifAi_sendCmdToCoproc_varName – dash-o Oct 17 '19 at 14:07
  • One possible approach is a named pipe. Have the parent process create a named pipe with `mkfifo \tmp\{some_name}` and pass the name to its children as an exported variable. Then one child can write to the pipe (just as it would to a regular file) and the other child can read from that pipe. Here's a simple example": https://www.linuxjournal.com/content/using-named-pipes-fifos-bash. – Andrew Vickers Oct 19 '19 at 17:50

0 Answers0