1

So I'm using the coproc command in a script to run a java program and to feed input to it, as follows:

#!/bin/bash

echo Script started.
coproc java -jar MultiThreadedFileProcessor.jar
echo start >&${COPROC[1]}
echo Script terminated.

I'd like to be able to write another script which can pass more input to this program (e.g. a command which will tell the program to run termination routines).

Is there any way that I can access the stdin of the coprocess from another script? My current attempt at a termination script is as follows:

#!/bin/bash

echo Script started.
echo terminate >&${COPROC[1]}
echo close >&${COPROC[1]}
echo Script terminated.

This gives me an ambiguous redirect error however, I'm guessing because COPROC[1] is only defined in the script that creates the coproc.

How else, if at all, can I write a script that will accomplish my goal of passing a line to the java program?

Mistborn
  • 11
  • 2

1 Answers1

0

COPROC is an array local to the first script - you will never be able to access that from another script. On Linux, you could access /proc/$pid/fd/$n with $pid being the pid of the first script and $n being the fd stored in ${COPROC[1]}. Both have to be communicated somewhere (a pid file and likewise an "fd file"), but in that case you might better be off creating a named pipe (mkfifo) and use that instead of creating a coproc in the first place. Something like this (not tested) codes:

Script 1:

mkfifo fifoname
exec 5>fifoname
java <fifoname &
echo close >&5

Script 2:

exec 5>fifoname
echo terminate >&5
echo close >&5

What is the close command supposed to do? The java program will see

close
terminate
close

Does that make sense to the java program?

Bodo Thiesen
  • 2,476
  • 18
  • 32