Python 代码因“无法连接到 X 服务器"而崩溃分离 ssh+tmux 会话时 [英] Python code crashes with "cannot connect to X server" when detaching ssh+tmux session

查看:39
本文介绍了Python 代码因“无法连接到 X 服务器"而崩溃分离 ssh+tmux 会话时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在远程机器上运行 Python 代码(我通过 ssh 进入),然后使用 Tmux.代码运行良好,直到我与远程机器断开连接.我通过 Tmux 连接的全部意义在于,即使我没有连接到远程机器,代码也能继续运行.稍后重新连接时,出现错误消息:

I run Python code on a remote machine (which I ssh into) and then use Tmux. The code runs fine UNTIL I disconnect from the remote machine. The whole point of my connecting via Tmux is so that the code continues to run even when I'm not connected to the remote machine. When I reconnect later, I have the error message:

: cannot connect to X server localhost:11.0

有谁知道为什么会发生这种情况或我该如何阻止它?

Does anyone have an idea why this is happening or how I can stop it?

推荐答案

cannot connect to X server localhost:11.0

...表示您的代码正在尝试(但失败)连接到 X 服务器——一个 GUI 环境——大概是通过您的 SSH 会话转发的.tmux 为终端应用提供会话连续性;它无法模拟 X 服务器.

...means that your code is trying (and failing) to connect to an X server -- a GUI environment -- presumably being forwarded over your SSH session. tmux provides session continuity for terminal applications; it can't emulate an X server.

如果您想完全阻止它建立任何 GUI 连接(也许,如果软件是这样编写的,甚至尝试),请在运行您的程序之前取消设置 DISPLAY 环境变量代码.

If you want to stop it from being able to make any GUI connection at all (and perhaps, if the software is thusly written, from even trying), unset the DISPLAY environment variable before running your code.

如果这会导致错误或异常,则生成的代码与导致您后续错误的代码相同.

If this causes an error or exception, the code generating that is the same code that's causing your later error.

如果您想创建一个仍然存在的 GUI 环境,您也可以使用 Xvfb 来实现.

If you want to create a fake GUI environment that will still be present, you can do that too, with Xvfb.

某些 Linux 发行版提供了 xvfb-run 包装器,以自动为您设置:

Some Linux distributions provide the xvfb-run wrapper, to automate setting this up for you:

# prevent any future commands in this session from connecting to your real X environment
unset DISPLAY XAUTHORITY

# run yourcode.py with a fake X environment provided by xvfb-run
xvfb-run python yourcode.py

顺便看看问题xvfb-run unreliable当并行调用多个实例时 以记录 xvfb-run 中存在的错误,以及可用于相同的修复.

By the way, see the question xvfb-run unreliable when multiple instances invoked in parallel for notes on a bug present in xvfb-run, and a fix available for same.

如果您想要一个 X 服务器,您实际上可以从中分离并重新附加到以后,让您运行具有类似于 tmux 为终端应用程序提供的功能的 GUI 应用程序,请考虑使用 X11vnc 或类似工具.

If you want an X server you can actually detach from and reattach to later, letting you run GUI applications with similar functionality to what tmux gives you for terminal applications, consider using X11vnc or a similar tool.

这篇关于Python 代码因“无法连接到 X 服务器"而崩溃分离 ssh+tmux 会话时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆