0

I have a long-running python program that I'm trying to run on a remote server.

I've looked at "How to keep processes running after ending ssh session?", " How to start process via SSH, so it keeps running?", "Run a persistent process via ssh", and a few other topics, but they don't seem to help.

I've tried running the python process with screen (via detaching a screen containing a background process) and nohup, but in both cases, when I exit the ssh session (which--I'm not sure if this matters--is run with X11 forwarding, since the python program is creating some graphics), the ssh session hangs.

The ssh process hangs even if I redirect stdin, stdout, stdout from/to /dev/null. Killing the ssh session kills the python process. When I kill the ssh, the following error message is printed on the remote server: g_dbus_connection_real_closed: Remote peer vanished with error: Underlying GIOStream returned 0 bytes on an async read (g-io-error-quark, 0). Exiting.

Furthermore, I don't actually want to redirect stdout or stderr to /dev/null, since I want to redirect them to a log file. So I didn't try running the python process as a daemon. (Perhaps it's bad that the logging is sent to stdout, I guess...)

What should I do, so that I can: (1) keep my process running after logging out, (2) redirect stdout/stderr to a log file?

(One thing which "worked" was suspending and then rerunning the ssh process [after it hangs] in the background, but what if I want to shut off my computer?)

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
jiangty
  • 311
  • 2
  • 9
  • 4
    You might consider using [`xpra`](http://xpra.org/) rather than `screen` if your program actually requires a X11 server. – Dan D. Nov 24 '12 at 08:46
  • ssh hangs, but what happens to a program you are trying to persist? – Raber Nov 24 '12 at 08:46
  • While the ssh is hanging, nothing happens. If I kill the ssh (via `Ctrl-c` or `~.`), the python program dies too. I'll look into `xpra`. – jiangty Nov 24 '12 at 08:50

2 Answers2

2

The X11 connection is indeed the problem. Screen takes care of keeping stdin/stdout/stderr connected, and it also protects the program from the HUP signal. However, it does not keep a virtual X server for it to write graphics on.

So the question is: what do you want with the graphics? If your program really needs to output them, you need to set up a (virtual) X server which it can continue to reach even after the connection is lost. You can connect to this virtual server with vnc, just like you can connect to your screen session.

Or you can make the program more suitable for running in the background, which means it must not use the X server. In that case, you probably want to output some files which you can then turn into graphics with a separate program when you want to see them.

Bas Wijnen
  • 1,288
  • 1
  • 8
  • 17
0

I thought sshd create new session leader bash for it's connection, so if you put your programmer at background and redirect your stdout/stderr ( >log 2>&1 ) , then even if you lose the connection ,the running bash will control your program.

Amir Naghizadeh
  • 383
  • 3
  • 14