如何让code tunnel在后台持续运行?关闭终端不中断连接
解决VS Code Tunnel在SSH断开后保持运行的问题
针对Fish Shell的Disown正确用法
你之前的disown操作失效,核心原因是Fish的disown默认行为和Bash不同——它仅会将进程从job列表移除,但不会让进程忽略SIGHUP信号(SSH断开时系统会给后台进程发这个信号终止运行)。正确操作如下:
- 启动隧道并后台运行:
code tunnel > ct_out 2> ct_err & - 用
jobs命令快速获取job编号(比查PID更直接):jobs - 执行带忽略SIGHUP参数的disown命令:
(把disown --ignore-hup %1%1换成你的实际job编号,或者直接用disown --ignore-hup PID指定进程ID)
Tmux的正确操作步骤
如果之前用Tmux没成功,大概率是没正确分离会话。按以下标准流程操作:
- 启动专属Tmux会话:
tmux new-session -s code-tunnel - 在Tmux窗口内直接运行隧道命令(无需加
&后台):code tunnel > ct_out 2> ct_err - 按下
Ctrl+B松开后再按D键,完成会话分离——此时即使SSH断开,Tmux内的进程仍会持续运行。 - 后续SSH登录后,重新接入会话查看状态:
tmux attach-session -t code-tunnel
更持久的方案:用Systemd管理进程
如果希望隧道能随系统启动自动运行,且异常时自动重启,推荐用systemd用户服务:
- 在
~/.config/systemd/user/目录下创建code-tunnel.service文件:[Unit] Description=VS Code Tunnel Service After=network.target [Service] Type=simple ExecStart=/usr/bin/code tunnel Restart=always RestartSec=5 StandardOutput=append:%h/code-tunnel-out.log StandardError=append:%h/code-tunnel-err.log [Install] WantedBy=default.target - 重新加载systemd用户配置:
systemctl --user daemon-reload - 启动服务并设置开机自启:
systemctl --user start code-tunnel.service systemctl --user enable code-tunnel.service - 查看服务状态或实时日志:
systemctl --user status code-tunnel.service journalctl --user -u code-tunnel.service -f
内容的提问来源于stack exchange,提问作者mjbeyeler
相关产品推荐
相关产品推荐

