1

I try to execute the following simplified batch job on my linux machine:

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#

The job should execute plot.py and save the terminal output to output.txt. The job seems to fail because the output.txt is empty.

If I execute the job manually, it succeeds:

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

The plot and the output.txt was created correctly.

Is there any logging where I could check why the batch job failed?

EDIT If I comment out the plt-lines in the script, the batch job finishes correctly.

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 According to the comment of @match, I changed the content of the file batch_job.txt to python plot.py 2>&1 | tee output.txt. Now I obtain the following error:

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

Why is the DISPLAY variable invalid? If I execute the script manually (as I stated in the top) in the shell /bin/sh that used by the batch job, it works.

Andy
  • 1,072
  • 2
  • 19
  • 33
  • What is the working directory used by `batch`? What happens if you use absolute filenames (e.g. `plt.savefig("/tmp/test.png")` or `tee /tmp/output.txt` ? – match Jan 13 '18 at 22:20
  • @match I tried to use absolute paths... but still only an empty `output.txt` is created. – Andy Jan 13 '18 at 22:57
  • 1
    The next thing to test is if a simple job runs successfully. Such as putting `echo "Hello" | tee output.txt` in `batch_job.txt` If that works, then I'd assume that the python script is crashing for some reason - in which case change `batch_job.txt` to contain `python plot.py 2>&1 | tee output.txt` to catch stderr as well as stdout. – match Jan 13 '18 at 23:10
  • @match Thanks for the tip how to catch stderr! Finally, I get an error. I paste it in EDIT2. – Andy Jan 13 '18 at 23:16

1 Answers1

0

For completeness, a search for the error in EDIT2 was successful: _tkinter.TclError: no display name and no $DISPLAY environment variable

Andy
  • 1,072
  • 2
  • 19
  • 33