使用cron作业执行python3文件。 [英] Executing python3 file with a cron job.
问题描述
我在/ home / valence /中有一个python3脚本,可以从Yahoo!获取当天的天气预报(摄氏温度的最高和最低温度值)。天气API。该文件看起来完全像这样:
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()
我执行它时效果很好。它使用正确的值创建或覆盖文件Forecast.txt,但是当我尝试使用cron执行以下cron作业时:
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
没有文件Forecast.txt是创建或修改的。
no file forecast.txt is created or modified.
所以我需要知道我做错了什么以及如何按预期进行这项工作。 cron作业并不是要每分钟执行一次(因为一天的预测在一天中仍然保持不变),但是现在我有了这种方式,这样我就可以看到更改而不必等待太多。
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.
注意:我是linux新手(正在使用Lubuntu)
Note: I am new to linux (I am using Lubuntu)
推荐答案
原因使用crontab作业<$ c $时,目录 / home / valence
中没有文件 forecast.txt
c> * * * * * /home/valence/Get_forecast.py 是cron不在目录 / home / valence
,然后在程序中以相对路径形式指定了文件 forecast.txt
。因此,将在其他位置创建文件。
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.
要获得预期的文件输出,可以使用以下crontab作业:
To get the expected file output, you can use the following crontab job:
* * * * * cd /home/valence && ./Get_forecast.py
这明确地将当前工作目录分配为 / home /价
,然后从那里执行脚本。
This explicitly assign the current working directory to be /home/valence
, and execute the script from there.
此外,要获得stdout和stderr的输出,添加重定向总是有帮助的
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
注意:
前面的两个crontab作业假定Get_forecast.py jexecutable并且已指定shebang。 ./ Get_forecast.py
部分始终可以替换为 python3 Get_forecast.py
或 / usr / bin / python3 Get_forecast.py
更清楚。
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.
这篇关于使用cron作业执行python3文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!