jpablobr.com

→ File Description Swapper

More on: linux, UNIX, Hack, tip, Mon Jan 24 15:18:20 +0000 2011

What if by mistake you redirect stdout or stderr to a file where the amount of data is huge?

I found this hack on google code which actually if you provide it with the /dev/pst/# of the offended PID, it will swap its file description to what ever you want. So pretty much you can redirect it to /dev/null. (quite cool!)

I did some minor modifications to it, - if interested - it can be downloaded here

Here is pretty much how it works:

Lets create a sample process. The following will echo out a number till the user manually kill the PID in some way.

% echo '#!/bin/bash\n n=0\n while true; do ((n++)); echo $n; sleep 1; done\n' > fsdtest-pid % chmod +x fsdtest-pid && ./fsdtest-pid

Now lets find the PID.

% ps aux | grep fsdtest-pid

This will give us the PID # we need for identifying the /dev/pts/#.

% ls -l /proc/$PID/fd/0

Now we can run the fsdwap.hs script like this:

% sudo fdswap.sh /dev/pts/$pst /dev/null $PID

Lets look at the fds again

% ls -l /proc/$pid/fd

To test it out and be sure the PID still alive you can strace it like so:

% strace -p $PID -e write

There you go!

blog comments powered by Disqus