Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing python3 file with a cron job.

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)

like image 915
Valence Avatar asked Apr 09 '15 00:04

Valence


People also ask

Can a cron job run a Python script?

You'll learn in a bit what this table refers to. You should use Cron any time you want to automate something, like an OS job or a Python script. Needless to say, but an automated Python script can do basically anything. On Linux and macOS, the Crontab consists of six fields.

What is the use of * * * * * In cron?

It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week .

How do I set up a cron job for a python script?

Open up a Terminal window and execute pwd and which python3 commands to get the absolute paths to your script folder and Python: Once you have these, enter the crontab -e command to edit a cron file, or to make one if it doesn’t exist: It will open a VIM editor — from there, click on the I key on your keyboard to enter insertion mode.

How do I use crontab in Python?

python-crontab is a Python module which provides access to cron jobs and enables us to manipulate the crontab file from the Python program. It automates the process of modifying the crontab file manually. To get started with python-crontab, you need to install the module using pip: 1. pip install python-crontab.

Where do my cron jobs run?

All cron jobs run in the context of the user for which they were scheduled. It is therefore always good practice to provide an absolute path to scripts and output files to avoid any confusion and cluttering.

When should I use cron?

You should use Cron any time you want to automate something, like an OS job or a Python script. Needless to say, but an automated Python script can do basically anything. On Linux and macOS, the Crontab consists of six fields.


1 Answers

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.

like image 91
greenqy Avatar answered Sep 28 '22 16:09

greenqy