You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过SSH执行脚本输出异常:行内容混乱原因咨询

Fixing Garbled Output When Running notmuch new Over SSH

Hey, I’ve dealt with this exact problem before—garbled line ordering and missing the expected "No new mail" tail when running notmuch new over SSH usually boils down to two key issues: output stream mixing and terminal buffering. Let’s break it down:

Why This Happens

1. Mixed stdout and stderr Streams

notmuch new sends some output to standard output (stdout) and other status messages to standard error (stderr). By default, SSH forwards both streams together without preserving strict ordering. When these streams interleave mid-transit, you end up with jumbled lines instead of clean, sequential output.

2. Block Buffering Instead of Line Buffering

When you run a command over SSH, the remote terminal treats it as a non-interactive session. Many programs (including notmuch) switch to block buffering in this mode—meaning they hold onto output until they’ve accumulated a full block of data instead of sending it line-by-line. This delays output and can cause lines to be merged or out of order when finally transmitted.

3. SSH Packet Forwarding Quirks

SSH optimizes output transmission by merging small packets. If notmuch isn’t flushing its buffer line-by-line, SSH might bundle partial lines together, leading to the messy output you’re seeing.

Solutions to Try

1. Merge stderr into stdout

Force all output to use a single stream to eliminate interleaving. Run the command like this:

notmuch new 2>&1

This redirects stderr (file descriptor 2) to stdout (file descriptor 1), ensuring all output arrives in the order it’s generated.

2. Force Line Buffering with stdbuf

Use the stdbuf tool to override notmuch’s buffering behavior and force line-by-line output:

stdbuf -oL -eL notmuch new
  • -oL: Enables line buffering for stdout
  • -eL: Enables line buffering for stderr

This makes notmuch flush each line immediately as it’s generated, which plays nicely with SSH’s forwarding.

3. Disable Buffering Entirely with unbuffer

If stdbuf doesn’t work, try unbuffer (part of the expect package) to turn off buffering completely:

unbuffer notmuch new

This tricks notmuch into thinking it’s running in an interactive terminal, so it won’t hold onto output blocks.

4. Verify Stream Behavior Locally

First, check if notmuch is actually splitting output between streams. Run this locally (not over SSH):

notmuch new > stdout.log 2> stderr.log

Open stdout.log and stderr.log—if stderr.log has content, stream mixing is definitely part of your problem.

内容的提问来源于stack exchange,提问作者res1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:36:44