If the program has already started, it's a matter of hacking (see below) to redirect its output. Instead, you can/should take precautionary measures beforehand.
I found it very useful to always run remote commands in a screen session. This allows you to:
- attach and detach to the session at will, and without disturbing the running programs. This means you can detach the session, logout, then login later and find all your "windows" and running programs as you left them (or, hopefully, finished computing). This also works when your ssh connection is dropped unexpectedly, or if you need to attach from multiple places at the same time (e.g. from your own laptop/machine via ssh, and directly from the university machine's terminal);
- have multiple (text mode) "windows" running on the same ssh connection, and switch between them (and their output) via keyboard shortcuts;
- have a scrollback buffer for each "window", so that even if you forgot to use
tee
, you can scroll up and see what the program has printed;
- set a session password, so that only you can reattach to it.
About the hacking part:
If you are stuck running a very long and important program, and can't restart it to use the method described above, there is still hope.
The open file descriptors for each process (including its current stdout) can be found via the /proc
filesystem. Using that, you can swap a file descriptor out and have the command switch to outputting somewhere else on the fly.
This script (fdswap.sh) uses gdb to swap out a file descriptor from under a running process, and there's an article explaining how to use it.
In short: find the pid of the running process, then run ls -l /proc/$pid/fd/0
to find where its current output goes, then use the above script to swap it out: fdswap.sh /dev/pts/$vty /path/to/new/output $pid
.
Historical reference: http://www.phrack.org/issues.html?issue=51&id=5