I have a python3 script located in /home/valence/ that gets the weather forecast for the current day (max and min temperature values in Celsius) from Yahoo! weather API. The file looks exactly like this:
#!/usr/bin/python3
from urllib import request
import json
url="https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D349859%20and%20u='c'&format=json&diagnostics=true&callback="
response=request.urlopen(url)
str_response = response.readall().decode('utf-8')
dic = json.loads(str_response)
dic["query"]["results"]["channel"]["location"]["region"]="R.M."
low=dic["query"]["results"]["channel"]["item"]["forecast"][0]["low"]
high=dic["query"]["results"]["channel"]["item"]["forecast"][0]["high"]
forecast=open("forecast.txt", "w+")
forecast.write("Minima: "+str(low)+" Maxima: "+str(high))
forecast.close()
It works fine when I execute it. It creates or overwrites the file forecast.txt with the right values, but when I try to use cron to execute with the following cron job:
* * * * * /home/valence/Get_forecast.py
no file forecast.txt is created or modified.
So I need to know what I am doing wrong and how to make this work as intended. The cron job is not meant to be executed every minute (because forecast for a day remains the same throughout the day), but for now I have it that way so I can see changes without having to wait much.
Note: I am new to linux (I am using Lubuntu)
The reason why you don't get the file forecast.txt
in directory /home/valence
when using crontab job * * * * * /home/valence/Get_forecast.py
is that the cron don't execute the command in directory /home/valence
and you specified the file forecast.txt
in relative path form in your program. So the file is created somewhere else.
To get the expected file output, you can use the following crontab job:
* * * * * cd /home/valence && ./Get_forecast.py
This explicitly assign the current working directory to be /home/valence
, and execute the script from there.
What's more, to get the output from stdout and stderr, add redirection is always helpful when something unexpected happens:
* * * * * cd /home/valence && ./Get_forecast.py > /tmp/forecast.log 2>&1
Note:
The previous two crontab job assumes Get_forecast.py jexecutable and with shebang specified. The ./Get_forecast.py
part can always be replaced with python3 Get_forecast.py
or /usr/bin/python3 Get_forecast.py
to be more clear.
What worked for me to run a python3 script is the next:
* * * * * /usr/local/bin/python3 /Users/gabriel/python/myScript.py
Using which python3
find the path to your python version and then add the path to your file.
I'd recommend providing an absolute path to the output file:
forecast=open("/tmp/forecast.txt", "w+")
Cron gets a little funny with paths / permissions and ownership of scripts that execute. Let me know if the file gets created under /tmp.
I use this tool to generate the command and I usually do something like
10 * * * * /home/valence/Get_forecast.py > /dev/null 2>&1
i think your cronjob is not at all running your program. Way to run a program is like this
python script.py
so your cronjob must look like this.
* * * * * python /home/valence/Get_forecast.py
or
* * * * * cd /home/valence && python Get_forecast.py
you can send all the outputs of a cronjob to a log file like this.
* * * * * cd /home/valence && python Get_forecast.py >> output_logs.op