Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

ssh

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?

like image 950
user1551817 Avatar asked Oct 03 '16 20:10

user1551817


1 Answers

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.

like image 158
Charles Duffy Avatar answered Oct 21 '22 01:10

Charles Duffy