Stephane
Stephane

Reputation: 31

Cron job not running with python import modules

I need to run a cron job on a python script to generate basemap plots.

  1. The script by itself runs ok manually.

  2. A simple print("Hello") at the start of the program with the rest commented out also runs ok on cron with
    */10 * * * * /usr/bin/python3 ~/PythonFiles/TestScript.py > /dev/null 2>&1 >>log.txt

I made the file an executable using chmod +x and added a shebang (#!/home/usr/anaconda3/bin/python) at the start of the program. I can monitor activity in the log file via a printed message at the start of the program too

  1. When I come to run the "normal" program which includes modules (urllib.request, datetime, matplotlib, basemap, pygrib, numpy, ...), the script then stops outputting anything to log.txt

So I suspect it is to do with modules and possibly their locations. I checked and they seem to have been installed in various places (.../pkgs, .../conda-meta, .../site-packages, etc...)

First of all, is what I suspect correct?
Secondly, how do I fix it so that cron knows where to find all the libraries to run the job?

Many thanks!

Upvotes: 2

Views: 2653

Answers (1)

Stephane
Stephane

Reputation: 31

I suspected it was to do with module location paths. After trawling through websites and tweaking inputs to cron, the following works!

SHELL=/bin/sh
HOME=/home/stephane
PYTHONPATH=/home/stephane/anaconda3/bin/python
PATH=/home/stephane/anaconda3/lib/python3.6/site-packages

*/2 * * * *  /home/stephane/anaconda3/bin/python ~/PythonFiles/TestScript.py >/dev/null 2>&1 >> log.txt

Note: matplotlib seems to need "import matplotlib as mpl; mpl.use('Agg')" to run off cron.

Thanks to all!

Upvotes: 1

Related Questions