Python code crashes with “cannot connect to X server” when detaching ssh+tmux session

ⅰ亾dé卋堺 提交于 2020-12-29 04:09:36

问题


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?


回答1:


cannot connect to X server localhost:11.0

...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.


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.


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

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

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.


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.



来源:https://stackoverflow.com/questions/39840184/python-code-crashes-with-cannot-connect-to-x-server-when-detaching-sshtmux-se

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!