结束 ssh 会话后在后台运行 python/matplotlib 时出现问题

发布于 2024-08-25 20:34:29 字数 1494 浏览 3 评论 0原文

我必须使用 VPN,然后从家里 ssh 到我的工作服务器,并且想要在后台运行 python 脚本,然后注销 ssh 会话。我的脚本使用 matplotlib 绘制了几个直方图,只要我保持连接打开,一切都很好,但如果我注销,我会在为脚本创建的日志文件中不断收到错误消息。

 File "/Home/eud/jmcohen/.local/lib/python2.5/site-packages/matplotlib/pyplot.py", line 2058, in loglog
    ax = gca()
  File "/Home/eud/jmcohen/.local/lib/python2.5/site-packages/matplotlib/pyplot.py", line 582, in gca
    ax =  gcf().gca(**kwargs)
  File "/Home/eud/jmcohen/.local/lib/python2.5/site-packages/matplotlib/pyplot.py", line 276, in gcf
    return figure()
  File "/Home/eud/jmcohen/.local/lib/python2.5/site-packages/matplotlib/pyplot.py", line 254, in figure
    **kwargs)
  File "/Home/eud/jmcohen/.local/lib/python2.5/site-packages/matplotlib/backends/backend_tkagg.py", line 90, in new_figure_manager
    window = Tk.Tk()
  File "/Home/eud/jmcohen/.local/lib/python2.5/lib-tk/Tkinter.py", line 1647, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display "localhost:10.0"

我假设它不知道在哪里创建我想要的数字,因为我关闭了 X11 ssh 会话。如果我在脚本运行时登录,我不会看到任何数字弹出(尽管这是因为我的脚本中没有 show() 命令),并且我认为 python 使用 tkinter 来显示数字。我创建图形的方式是,

loglog()
hist(list,x)
ylabel('y')
xlabel('x')
savefig('%s_hist.ps' %source.name)
close()

脚本需要一些初始输入,所以我在后台运行它的方式是

python scriptToRun.py << start>& logfile.log&

有没有办法解决这个问题,或者我只需要保持 ssh 进入我的机器?

谢谢。

I have to VPN and then ssh from home to my work server and want to run a python script in the background, then log out of the ssh session. My script makes several histogram plots using matplotlib, and as long as I keep the connection open everything is fine, but if I log out I keep getting an error message in the log file I created for the script.

 File "/Home/eud/jmcohen/.local/lib/python2.5/site-packages/matplotlib/pyplot.py", line 2058, in loglog
    ax = gca()
  File "/Home/eud/jmcohen/.local/lib/python2.5/site-packages/matplotlib/pyplot.py", line 582, in gca
    ax =  gcf().gca(**kwargs)
  File "/Home/eud/jmcohen/.local/lib/python2.5/site-packages/matplotlib/pyplot.py", line 276, in gcf
    return figure()
  File "/Home/eud/jmcohen/.local/lib/python2.5/site-packages/matplotlib/pyplot.py", line 254, in figure
    **kwargs)
  File "/Home/eud/jmcohen/.local/lib/python2.5/site-packages/matplotlib/backends/backend_tkagg.py", line 90, in new_figure_manager
    window = Tk.Tk()
  File "/Home/eud/jmcohen/.local/lib/python2.5/lib-tk/Tkinter.py", line 1647, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display "localhost:10.0"

I'm assuming that it doesn't know where to create the figures I want since I close my X11 ssh session. If I'm logged in while the script is running I don't see any figures popping up (although that's because I don't have the show() command in my script), and I thought that python uses tkinter to display figures. The way that I'm creating the figures is,

loglog()
hist(list,x)
ylabel('y')
xlabel('x')
savefig('%s_hist.ps' %source.name)
close()

The script requires some initial input, so the way I'm running it in the background is

python scriptToRun.py << start>& logfile.log&

Is there a way around this, or do I just have to stay ssh'd into my machine?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

や三分注定 2024-09-01 20:34:29

我相信你的 matplotlib 后端需要 X11。查看您的 matplotlibrc 文件以确定您的默认值(从错误中,我打赌 TkAgg)。要在没有 X11 的情况下运行,请使用 Agg 后端。可以在 matplotlibrc 文件中进行全局设置,也可以通过将以下内容添加到 python 程序来逐个脚本进行设置:

import matplotlib
matplotlib.use('Agg')

I believe your matplotlib backend requires X11. Look in your matplotlibrc file to determine what your default is (from the error, I'm betting TkAgg). To run without X11, use the Agg backend. Either set it globally in the matplotlibrc file or on a script by script by adding this to the python program:

import matplotlib
matplotlib.use('Agg')
童话 2024-09-01 20:34:29

看起来默认情况下你是在交互模式下运行的,所以 matplotlib 想要首先将所有内容绘制到屏幕上,但这当然是它做不到的。

尝试将其放在

ioff()

脚本的顶部,同时更改后端。

参考: http://matplotlib.sourceforge.net/api/pyplot_api.html #matplotlib.pyplot.ioff

It looks like you're running in interactive mode by default, so matplotlib wants to plot everything to the screen first, which of course it can't do.

Try putting

ioff()

at the top of your script, along with making the backend change.

reference: http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.ioff

苏大泽ㄣ 2024-09-01 20:34:29

抱歉,如果这是一个愚蠢的答案,但如果您只是运行控制台会话,“屏幕”还不够吗?可拆卸会话等

Sorry if this is a stupid answer, but if you're just running a console session, would 'screen' not suffice? Detachable sessions, etc.

物价感观 2024-09-01 20:34:29

如果您在 *nix 操作系统上运行,则问题是您的会话被终止,并且当您断开连接时,所有需要会话的进程也会被终止。更具体地说,您的所有进程都会收到 SIGHUP(信号挂起)。 SITHUP 的默认处理是终止进程。如果您希望脚本继续,则需要忽略该信号。最简单的方法是假设您通过命令行启动脚本,然后使用 nohup 命令运行脚本:

nohup python scriptToRun.py << start>& logfile.log&

nohup 通常将标准输出和标准错误发送到当前目录中的 nohup.out 文件。由于您已经重定向输出,因此不会创建 nohup.out 。

If you are running on a *nix OS the problem is your session is terminated and all processes requiring a session are also terminated when you disconnect. More specifically all your processes are sent a SIGHUP (signal hang-up). The default handling of SITHUP is to terminate the process. If you want you script to continue it needs to ignore the signal. The easiest way to do that assuming you start your script via the command line it to run it using the nohup command:

nohup python scriptToRun.py << start>& logfile.log&

nohup normally sends standard out and standard error to the file nohup.out in the current directory. Since you're redirecting already output nohup.out will not be created.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文