2

I am using xubuntu 18.01

I have a python program that that scrapes weather data and saves the files as a csv. It works perfectly running the command weatherdata in terminal after i gave it permission using chmod +x weatherdata.

I would like this to run every 2 weeks using cron. But nothing happens after i set it up.

I am using the NANO cron editor

I have set the cronjob up trying PATH variables, SHELL=/bin/bash/. P Putting the /bin/bash in front of the command... all to no avail....

# crontab -e
SHELL=/bin/bash
MAILTO= mygmail@gmail.com (removed my actual email for privacy)
PATH=/home/luke/bin/
# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command


34 * * * * /bin/bash /home/luke/bin/weatherdata

I expected the weatherdata python file to be executed every hour at 34 minutes past the hour.

Nothing happens, I don't get an email or anything.


Edit:

I changed to top to

SHELL=/bin/bash
#MAILTO=mygmail@gmail.com (commented it out)
PATH=/home/luke/bin/:/usr/sbin:/usr/bin:/sbin:/bin

what i get after running: 15 * * * * /bin/bash /home/luke/bin/weatherdata > /tmp/weatherlog 2>&1 as suggested in the comments.

Traceback (most recent call last):
  File "/home/luke/Documents/Duka/Master_River_Levels_LOOP.py", line 9, in <module>
    import pandas as pd
ImportError: No module named pandas
Traceback (most recent call last):
  File "/home/luke/Documents/Duka/Temp_Mean_Weather_loop.py", line 9, in <module>
    import pandas as pd
ImportError: No module named pandas

I need to import a few modules for the python script to run, the first one being panadas.

Lucas Muller
  • 101
  • 9
  • 1
    You've removed `/bin` and `/usr/bin` from your PATH, so any script is likely to behave poorly. Also, unless you've taken steps to configure your system for it, mail to external gmail addresses will fail. Try the troubleshooting guide on the [cron tag wiki](https://stackoverflow.com/tags/cron/info), especially the part about logging: `34 * * * * /bin/bash /home/luke/bin/weatherdata > /tmp/weatherlog 2>&1` and look at /tmp/weatherlog after it's executed. – that other guy Feb 07 '19 at 03:45
  • Thank you @thatotherguy. Apparently cron doesn't have pandas or any other python libraries installed. I am not sure how to do that. I posted an edit to my question with the weatherlog file you suggested for reference. – Lucas Muller Feb 07 '19 at 18:11
  • 1
    Maybe you normally set PYTHON_PATH in your .bashrc or similar, which wouldn't be automatically sourced by cron? – that other guy Feb 07 '19 at 18:15
  • @thatotherguy thank you for the advice. I will need to find some resources on that and read up. – Lucas Muller Feb 09 '19 at 00:02

2 Answers2

2

The crontab -e job looks fine.

Why not just use:

34 * * * * /home/luke/bin/weatherdata

Or else set a sh file ... eg. myfile.sh

#!/usr/local/env bash
/home/luke/bin/weatherdata

Then crontab -e reads

34 * * * * /home/luke/myfile.sh

Cron works a dream unless your system admin is blocking it.

Larry Wall
  • 46
  • 7
0

As @thatotherguy suggested it was my path that was the problem.

1) I ran echo $PATH in terminal and got back:

/home/luke/anaconda3/bin:/home/luke/anaconda3/condabin:/home/luke/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

2) I copy and pasted the above path to replace: PATH=/home/luke/bin/:/usr/sbin:/usr/bin:/sbin:/bin with

PATH=/home/luke/anaconda3/bin:/home/luke/anaconda3/condabin:/home/luke/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin 

3) I changed PATH=/ to PATH=$PATH:/

Now the cronjob executes perfectly.

Lucas Muller
  • 101
  • 9