The problem is quite simple: I find useful to have the possibility to switch on (and off) the output of some running program in each moment I need. To be more precise I want to be free to redirect their standard output (and error) from the current shell to another one, to /dev/null
, to a file or back to the original shell.
I search for something direct (and safe) that takes advantage from the fact I know in advance that I will switch the outputs on and off. I post an answer too.
Below my first attempt. Let's suppose I work in the shell number 35.
$ who am I # the command line to ask where am I
myuser pts/35 ... # The answer
My try starts with a symbolic link to that terminal
ln -s /dev/pts/35 MyOutput # The command to link
The idea is to launch the program with the redirection set to that link
./Execute_program > MyOutput
It works, it redirects the output to my terminal, but when I give the command to change the link,
ln -sf /dev/null MyOutput
the link changes, but the redirection doesn't change how I hoped. It doesn't change for the running process. If I launch a new program in the same way, the redirection follows the new prescription of the link.
If I start again and I set the link to /dev/null
the output is suppressed as expected, but again when I change the link the redirection doesn't change.
If I do a link to a link I get the same result.
The use of hard link
doesn't change the situation.
Sadly the shell expands the command line at launch time.
Is there any possibility to do the job in a similar way or have I to find how to communicate to the process (or the PPID ones) that the link is changed?
Notes:
This is a question of the series "How to redirect the output of a running process?", but it doesn't start from the point "Ops I launched the program and I forget to redirect the output. Now I want to do it", but from the opposite: "How can I launch a program with the explicit aim to redirect elsewhere the output at a later stage?".
I intend to use a such procedure when it is not possible (or convenient) to modify the source code of the program.
I read that it is possible to disown
the process, use screen
and transfer form a screen to another... or to use a debugger, to intercept the process... All those solutions could function with a good amount of experience and effort, but some of them present a percentage of risk too.
cat
, the program will hang again when the pipe gets full (64kB on Linux). – Stéphane Chazelas May 12 '14 at 08:46/dev/null
. That is what I want. The program remains hanged if I don't start calling the 1st cat when I launch the program and there's an output bigger than the buffer. – Hastur May 12 '14 at 11:13