Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to redirect a running process output to a file and log out
touch /tmp/stdout
touch /tmp/stderr
gdb -p $!
# In GDB
p dup2(open("/tmp/stdout", 1), 1)
p dup2(open("/tmp/stderr", 1), 2)
# Back in shell

This comment has been minimized.

Copy link

@thomasluce thomasluce commented Jan 17, 2011

I've not seen it done quite like this before. I like it!

It might be a bit cleaner with something like nohup $you_command_and_arguments_here 2>/tmp/stderr 1>/tmp/stdout to avoid the overhead of all the ptrace's that go on in gdb. Also, disown's implementation is defined to be shell-specific (in other words, do what works for your shell), whereas nohup uses system-level commands to abandon it's parent, so you are less likely to have strange side-effects if you try it on a non-bash shell.


This comment has been minimized.

Copy link
Owner Author

@zaius zaius commented Jan 17, 2011

Yeah it's definitely cleaner to do it that way, if you're organized :) I would make it even a simpler and just run it in screen or tmux. Though I know I regularly start long-running tasks before realizing how long they are going to take, so this is a quick hack to get around it.


This comment has been minimized.

Copy link

@ogier ogier commented Jan 17, 2011

Thomas, I think you missed the point of the gist. This makes sense if you have a process already running and later realize you need to disown it without losing its output. I for one run into this problem often and I really like this solution.


This comment has been minimized.

Copy link

@nnutter nnutter commented Jan 17, 2011

This was linked on Hacker News and in the comments there julian37 pointed out that someone created a bash script to do this called "Dupx".

Edit: And of course, thank you to bringing this tool to my attention. Very useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment