mblw
mblw

Reputation: 1798

Bash I/O redirection

The main question of my current task is to start process from Tcl environment (using exec cmd.) with I/O redirection to parent process (Tcl) and when parent process unexpectedly stopped I'd like to automatically stop the child process. But there is a caveat: for I/O redirection case child process doesn't stop. The problem is complexed because child process can't be modified.

I've found the next thread How to make child process die after parent exits? where solution has been found for C language and without I/O redirection.

So for example, I use the next command in Tcl:

exec ping $SERVER_NAME >@ stdout 2>@ stderr

This command redirect output of stdout and stderr to the parent process (Tcl). But I need to run something like that:

exec sh -c "set -o monitor; ping $SERVER_NAME >@ stdout 2>@ & read dummy; kill %1"

And in the command above output of stdout and stderr goes into "sh" process but I'd like to get it in my parent process. So I have the several question:

  1. What mean symbol "@" in the following command (may be you could get a link)?
  2. How to make I/O redirection to the Tcl process?
  3. How to stop child process (ping in example) if Tcl process unexpectedly stopped?

Upvotes: 0

Views: 291

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137767

The meaning of >@ stdout is to send the output of the command to the Tcl process's standard out without collecting it. The meaning of 2>@ stderr is to send the standard error stream of the command to the Tcl process's standard error without collecting it (if it collected something non-empty, it would lead to an error). Thinking about replicating that in a subprocess invokation, even in bash, makes my head hurt! I suspect that you can't quite do it that way.

It's probably easier to do your processing like this:

set pid [exec exec ping $SERVER_NAME >@ stdout 2>@ stderr &]

# and when you want to stop it...
exec kill $pid

Doing a more complete job will require using, say, the TclX package's signal command.

Upvotes: 0

Related Questions