Reputation: 9004
I want to execute Program_A, and have it output examined by Program_B. e.g.
$ Program_A | Program_B
Within Program_B, I would like to be able to terminate Program_A if a certain condition matched.
I am looking for a way to do this in BASH (have a solution in python with popen).
Also, pidof Program_A
and alike are not a good solution since there can be many instances of it and only a particular one shall be terminated.
Upvotes: 0
Views: 114
Reputation: 3967
You can get the process id of Program_A in the Program_A itself and print that as the first line and other outputs to Program_B. If you do this then you can kill the Program_A with kill call from Program_B
Upvotes: 1
Reputation: 1239
In Program_B you could close()
stdin when you detect the closing condition. It will cause Program_A to receive error upon write()
and possibly terminate.
Now, the Program_A may choose to ignore those errors (I don't know if it's your own application or a pre-compiled tool) and here comes the funny part. In Program_B you can examine where does your stdin come from:
my_input=`readlink /proc/$$/fd/0`
then, if it's a something like "pipe:[134414]", find any other process whose stdout equals
case "$my_input" in
pipe:*)
for p in /proc/[0-9]*
do
if [ "$my_input" = "`readlink $p/fd/1`" ]
then
bad_guy=`sed -e 's:.*/::' <<< "$p"`
echo "Would you please finish, Mr. $bad_guy ?!"
kill $bad_guy
break
fi
done
;;
*)
echo "We're good";;
esac
Upvotes: 0