26

Here is the situation. I left my pc at home doing an rsync from a 2TB hard drive to another 2TB hard drive (it's going to take a while since they are both USB 2.0). I am now at work and I have ssh-ed into my home pc. If I do ps aux | grep rsync I can see the following:

1000  7214 18.8  0.1  30636  1368 pts/0 S+   00:52 134:00 rsync -vr /media/master /media/slave

However I want to see exactly what rsync is doing. When I was at home, the standard output was shown in my terminal and the verbose mode of rsync showed which files were currently being copied. Is there any way to read stdout for another pts?

$ ps -t pts/0
7214 pts/0    02:14:42 rsync

I did a little bit of googling and it seems that /proc/pid/fd may hold the answer but I'm not sure about this...

P.S. I have sudo privileges of course.

mulllhausen
  • 2,668
  • Even if it's possible, once it's been shown on the terminal it's no longer in the pty. – Random832 Jun 10 '11 at 03:32
  • 2
    yeah i guess it depends if some kind of stream needs to be re-directed or if it can just be passively 'read' by my terminal. i should have just done screen rsync ... at home but i didnt expect it to take so long when i started. – mulllhausen Jun 10 '11 at 03:38
  • If you just want to see what rsync is doing, you can just attach a strace to the process: strace -p 7214 – Random832 Jun 10 '11 at 03:43
  • i tried strace but it gives a MASSIVE output (probably due to the high speed of the rsync process) like this: read(0, "\n\204C\320\234\33\36\376\36\244H\242\3\1\231\353F[G[SN\244M\0232(\213\247GKh"..., 4092) = 4092 select(1, [0], [], NULL, {60, 0}) = 1 (in [0], left {59, 999998}) – mulllhausen Jun 10 '11 at 03:46
  • I found it! strace has options you can use, will post answer shortly. – Random832 Jun 10 '11 at 03:53
  • 1
    With a bit more experience you would know just to start thes long running commands in tmux or screen. – Anthon Apr 11 '16 at 22:40
  • Not a duplicate at all; this question gives the hack to redirect a running process' stdin as well as stdout/stderr; this saves the day for me. A bunch of stinky strace calls wouldn't. – ulidtko Jan 03 '17 at 14:30

5 Answers5

23

Run gdb -p 7214, and do:

p dup2(open("/dev/pts/your-pts-number", 1), 1)
detach
quit

This isn't exactly obvious, but it redirects the stdout from the process you attached to to the path given in the first line (in this case, your desired pts).

15

The strace tool allows you to get a copy of the output going to a particular file descriptor (in this case, stdout and stderr).

strace -e write=1,2 -e trace=write -p 7214

This will appear in hex dump format.

Random832
  • 10,666
  • this would be a really elegant solution if it could produce a human readable output, but the output is unreadable - its all in hex? all lines start with something like write(3, "jh\260)\340\221j\16S\203B\202... which im assuming is due to the writing of data from one drive to the other – mulllhausen Jun 10 '11 at 05:57
  • Perfect! Very usefull for debugs! And the right column show also ASCII, so its not just HEX. Thanks. – DrBeco Oct 12 '13 at 18:50
6

You can detach the rsync process from its terminal and move it to the current one. See How can I disown it a running process and associate it to a new screen shell?. The fundamental approach is similar to the gdb command in John Flatness's answer, but tools such as neercs and retty are more robust.

Make sure to import the process into a screen or tmux session, so you don't have to detach it yet again (unless you use neercs, which provides this functionality already).

Another approach is to check what the process is doing right now by looking at what files it has open. lsof will show you that.

lsof -p7214
1

If you know you're going to want to do this in advance ...

Create a script session on your home computer (using -f to flush buffer):

script -f output.txt
rsync -vr /media/master /media/slave

(Ctrl+D to finish the script session when get home)

At work you can track output.txt:

tail -f output.txt
Anthon
  • 79,293
Wayne
  • 11
  • If you want to catch characters as the user types, you can prefix the script command with stty -icanon &&. Otherwise you'll only get output when the user presses enter. Also be aware that this grabs everything that gets written to the terminal, not just stdout. – MatrixManAtYrService Oct 07 '18 at 18:41
1

In BSD check the watch command.

Otherwise please check the following example using strace (where 7214 is your PID):

strace -e trace=write -s1000 -fp 7214 2>&1 \
| grep --line-buffered -o '".\+[^"]"' \
| grep --line-buffered -o '[^"]\+[^"]' \
| while read -r line; do
  printf "%b" $line;
done

For further explanation or more examples, check: How to view the output of a running process in another bash session?

kenorb
  • 20,988