Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I run a lot of long running processes (simulations) that print progress to STDOUT. I occasionally forget to redirect to STDOUT to a file I can grep, and it's usually too far along to restart.

QUESTION: Without stopping the process, is there a way I can hook into another STDOUT?

These are always running in GNU screen with ZSH on OS X 10.7.3.

share|improve this question
up vote 4 down vote accepted

There is a clever hack mentioned here that uses GDB to attach to the process, and a utility named dupx wraps up this functionality.

From the dupx manpage:

Dupx is a simple utility to remap files of an already running program. Shells like Bash allow easy input/output/error redirection at the time the program is started using >, < - like syntax, e.g.: echo 'redirect this text' > /tmp/stdout will redirect output of echo to /tmp/stdout.

Standard shells however do not provide the capability of remapping (redirecting) of output (or input, or error) for an already started process. Dupx tries to address this problem by using dup(2) system call from inside gdb(1). Dupx is currently implemented as a simple shell wrapper around a gdb script.

share|improve this answer
Thanks! I had to make some modifications, but it does exactly what I need. – jmdeldin Mar 24 '12 at 20:45
You're welcome! Glad you got it to work. – Matt Eckert Mar 24 '12 at 20:48
reredirect is very neat for this too. – lemonsqueeze May 6 at 2:24

Use screen's log command (!)

Since the process is running in a screen session already it's just a matter of telling screen to log the output of that window:

Switch to the script's window, C-a H to log.
Now you can :

$ tail -f screenlog.2 | grep whatever

From screen's man page:

log [on|off]

Start/stop writing output of the current window to a file "screenlog.n" in the window's default directory, where n is the number of the current window. This filename can be changed with the 'logfile' command. If no parameter is given, the state of logging is toggled. The session log is appended to the previous contents of the file if it already exists. The current contents and the contents of the scrollback history are not included in the session log. Default is 'off'.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.