大网数据广告活动
服务器运维问答

Debugging rsync over SSH in verbose mode using tee to redirect stdout to log file

· 52CE 我爱测

I have been having issues with various rsync operations over SSH, on a particular machine on our network, failing and giving errors like: rsync: connection unexpectedly closed (45482 bytes received so far) [generator] I found a s…

问题原文

原帖链接 · PicoutputCls · CC BY-SA 3.0

I have been having issues with various rsync operations over SSH, on a particular machine on our network, failing and giving errors like:

rsync: connection unexpectedly closed (45482 bytes received so far)  [generator]

I found a similar post (rsync - unexplained error) for which one of the answers suggested running the SSH command used by rsync in verbose mode and redirecting the output, in my case as follows:

rsync -av -e 'bash -x -c "ssh -p 22 -vvvv $0 $@ 2>/tmp/rsync-ssh.stderr | tee /tmp/rsync.stdout"' --rsync-path='sudo rsync' "[email omitted]:/media/remote_volume/" "/media/local_volume"

This rsync command seems to run fine for a while but eventually fails with the error:

tee: standard output: Resource temporarily unavailable

I am guessing it is likely failing for a similar reason as is described in the answer to the following question: Why is this tee losing stdout? However, it doesn't seem clear to me how I should change the SSH command in my case to remidy the issue.

Does anyone know way of making SSH play nice with tee in this scenario?

已采纳解答

原帖链接 · PicoutputCls · CC BY-SA 3.0

Having done a bit more reading on the solution discussed in the answer to the question "Why is this tee losing stdout?", it seems like the crucial change is replacing the pipe with redirection to a subshell containing the command that would otherwise be being piped into. (Thus, replacing: command1 | command2 with: command1 > >( command2 ))

The reason for this is that by redirecting to a subshell, rather than directly piping into the next command, one is forcing the subshell to handle any EAGAIN error codes returned. As discussed in this FreeBSD message board message: bin/164947: tee looses data when writing to non-blocking file descriptors (also linked in the answer to the other question) tee and other system binaries can be quite bad at handling retries themselves.

For the rsync command above, the following alterations seem to fix my original issue:

rsync -av -e 'bash -x -c "ssh -p 22 -vvvv $0 $@ 2>/tmp/rsync-ssh.stderr > >( tee /tmp/rsync.stdout )"' --rsync-path='sudo rsync' "[email omitted]:/media/remote_volume/" "/media/local_volume"

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-11-23)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。