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

Unison无法稳定检测新文件与变更的原因及解决方法

前言

我用Unison在本地MacBook Pro和远程服务器之间同步文件,但同步表现不稳定:有时正常工作,有时检测不到变更。Unison偶尔无法识别远程目录新增的文件,本地变更大多能识别,但偶尔也会失效。比如它没检测到文件cs241/a1/a1q5/self-modifying.mips。终止进程后用nohup脚本重启,问题暂时解决,但几分钟后又无法检测远程变更,不过本地变更仍能同步。

问题

为什么Unison无法稳定检测新文件?怎么确保它始终能识别到变更?

补充信息

  • Unison版本:2.53.5
  • 配置包含repeat = watch,用于实时监控

欢迎提供见解或建议


Unison日志摘录

Unison 2.53.5 (ocaml 5.1.0): Contacting server...
Connected [//local-machine.local//Users/username/uw -> //remote-server//remote/path]

Looking for changes
  Waiting for changes from server
Reconciling changes
         <---- new file   **cs241/a1/a1q5/self-modifying.mips**

1 items will be synced, 0 skipped
0 B to be synced from local to remote-server
36 B to be synced from remote-server to local
Propagating updates
Unison 2.53.5 (ocaml 5.1.0) started propagating changes at 20:43:21.62 on 19 May 2024
[BGN] Copying cs241/a1/a1q5/self-modifying.mips from //remote-server//remote/path to /Users/username/uw
[END] Copying cs241/a1/a1q5/self-modifying.mips
Unison 2.53.5 (ocaml 5.1.0) finished propagating changes at 20:43:21.73 on 19 May 2024, 0.119 s
Saving synchronizer state
Synchronization complete at 20:43:21  (1 item transferred, 0 skipped, 0 failed)
Looking for changes
  Waiting for changes from server
Reconciling changes
Nothing to do: replicas have not changed since last sync.

运行中的进程

ps aux | grep unison
username      3132   0.0  0.0 410724048   1344 s001  S+    9:16pm   0:00.00 grep unison
username      3019   0.0  0.1 411474560  12800 s001  SN    8:43pm   0:00.94 /opt/homebrew/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/Resources/Python.app/Contents/MacOS/Python /opt/homebrew/bin/unison-fsmonitor
username      3018   0.0  0.0 411175072   4048 s001  SN    8:43pm   0:00.13 ssh -l remoteuser remote.server.com -e none unison -server __new-rpc-mode
username      3011   0.0  0.0 412093776   4832 s001  SN    8:43pm   0:00.32 unison sync.prf

...

添加至~/.zshrc的脚本

#!/bin/zsh

#Define the log file path
LOG_FILE="/Users/username/Library/Application Support/Unison/unison.log"

#Check if a Unison process is already running
if pgrep -f "unison sync.prf" > /dev/null; then
  echo "Unison UW Sync process is already running. Exiting."
  exit 1
else
  #Start the Unison process with nohup
  nohup unison sync.prf > "$LOG_FILE" 2>&1 &
  echo "Unison UW Sync process started."
fi

Sync.prf配置文件

#Root directories
root = /Users/username/uw
root = ssh://user@linux.rest//directory

#Automatically synchronize changes
auto = true
batch = true
repeat = watch

#Ignore dot files
ignore = Name .*

已尝试的操作

  • 确认unison-fsmonitor正在运行
  • 检查Unison日志是否存在错误或警告
  • 验证网络连接与权限
  • 多次重启Unison进程
  • 用pkill -f unison终止进程,再通过nohup unison sync.prf > /Users/username/Library/Application Support/Unison/unison.log 2>&1 &重启

解决方案建议

1. 检查远程端文件监控机制

Unison的repeat = watch依赖两端的文件系统监控,远程端可能存在配置问题:

  • 登录远程服务器,确认安装的unison-fsmonitor版本与本地Unison(2.53.5)匹配
  • 在远程端手动运行unison-fsmonitor,观察是否能正常输出文件变更事件
  • 若远程是Linux系统,确保inotify-tools已安装,可切换使用inotify作为监控后端

2. 调整Unison监控参数

  • 在sync.prf中添加watchdelay = 1,缩短监控轮询间隔
  • 尝试将repeat = watch替换为repeat = 1,改用定时轮询模式(实时性稍弱但稳定性更高)

3. 修复SSH连接稳定性

远程检测失效可能和SSH连接超时有关:

  • 在本地~/.ssh/config中添加远程服务器的保活配置:
    Host linux.rest
      ServerAliveInterval 30
      ServerAliveCountMax 10
    
  • 改用tmux或screen维持SSH会话,避免后台Unison进程因会话断开被终止

4. 清理Unison状态文件

同步状态文件损坏可能导致检测异常:

  • 停止所有Unison进程
  • 删除本地~/.unison/下的sync.prf.*状态文件
  • 删除远程端~/.unison/目录下的对应状态文件
  • 重启Unison,让它重新构建同步状态

5. 排查权限与特殊文件

  • 确认远程新增文件的权限允许Unison读取(运行用户需有读权限)
  • 暂时移除ignore = Name .*配置,测试是否是隐藏文件规则影响了变更检测
  • 排查是否存在符号链接、特殊设备文件等可能干扰监控的文件

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 18:42:37