11

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?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
user1551817
  • 6,693
  • 22
  • 72
  • 109

1 Answers1

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

Community
  • 1
  • 1
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 1
    Very useful thanks. I'm not sure why my code even tried to make a GUI connection as I don't ask it to show() anything. But now I know where the problem is, I can experiment a little. Thanks! – user1551817 Oct 03 '16 at 22:27
  • 1
    Ahh. Often, initializing a GUI library is enough to try to connect without actually going as far as displaying any windows. – Charles Duffy Oct 03 '16 at 22:35