使用 matplotlib 时批处理作业失败

Batch job fails when using matplotlib

我尝试在我的 linux 机器上执行以下简化的批处理作业:

root@kali:~/Test_plots# ls
batch_job.txt  plot.py
root@kali:~/Test_plots# more plot.py 
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,4)
y = np.sin(x)
plt.plot(x,y)
plt.savefig("test.png")
print "done"
root@kali:~/Test_plots# more batch_job.txt 
python plot.py | tee output.txt
root@kali:~/Test_plots# batch < batch_job.txt 
warning: commands will be executed using /bin/sh
job 30 at Sat Jan 13 22:35:00 2018
root@kali:~/Test_plots# atq
30  Sat Jan 13 22:35:00 2018 = root
root@kali:~/Test_plots# atq
root@kali:~/Test_plots# ls
batch_job.txt  output.txt  plot.py
root@kali:~/Test_plots# more output.txt
root@kali:~/Test_plots#

作业应执行 plot.py 并将终端输出保存到 output.txt。作业似乎失败了,因为 output.txt 是空的。

如果我手动执行作业,它会成功:

root@kali:~/Test_plots# more batch_job.txt 
python plot.py | tee output.txt
root@kali:~/Test_plots# which sh
/bin/sh
root@kali:~/Test_plots# sh
# python plot.py | tee output.txt
Fontconfig warning: ignoring UTF-8: not a valid region tag
done
# exit
root@kali:~/Test_plots# ls
batch_job.txt  output.txt  plot.py  test.png
root@kali:~/Test_plots# more output.txt 
done

情节和 output.txt 已正确创建。

是否有任何日志记录可以让我检查批处理作业失败的原因?

编辑 如果我注释掉脚本中的 plt 行,批处理作业将正确完成。

root@kali:~/Test_plots# ls
batch_job.txt  plot.py
root@kali:~/Test_plots# more plot.py 
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,4)
y = np.sin(x)
#plt.plot(x,y)
#plt.savefig("test.png")
print "done"
root@kali:~/Test_plots# batch < batch_job.txt 
warning: commands will be executed using /bin/sh
job 34 at Sun Jan 14 00:02:00 2018
root@kali:~/Test_plots# atq
root@kali:~/Test_plots# ls
batch_job.txt  output.txt  plot.py
root@kali:~/Test_plots# more output.txt 
done
root@kali:~/Test_plots# 

EDIT2 根据@match的评论,我把文件batch_job.txt的内容改成了python plot.py 2>&1 | tee output.txt。现在我收到以下错误:

Fontconfig warning: ignoring UTF-8: not a valid region tag
Traceback (most recent call last):
  File "plot.py", line 5, in <module>
    plt.plot(x,y)
  File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 3307, in plot
    ax = gca()
  File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 950, in gca
    return gcf().gca(**kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 586, in gcf
    return figure()
  File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 535, in figure
    **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 81, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 89, in new_figure_manager_given_fig
ure
    window = Tk.Tk()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1822, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

为什么DISPLAY变量无效?如果我在批处理作业使用的 shell /bin/sh 中手动执行脚本(如我在顶部所述),它会起作用。

为了完整性,在 EDIT2 中搜索错误是成功的: