我已经在python虚拟环境中安装了django-crontab。我正在尝试添加一行到谷歌工作表每天,我知道的功能,所以我只需要一个cron作业来触发它。
python 3.6.7
django 2.0.9
django-crontab 7.0.1下面是我的文件夹结构:
├── api
│ ├── utils
│ | └── cron.py
|---manage.py这是我的cron.py
# For Google API to sheets
import pygsheets
# The ID of spreadsheet.
SPREADSHEET_ID = 'abc123'
def addToGraphs():
# Use service credentials
print('Attempt CRON')
gs = pygsheets.authorize(service_account_file='credentials.json')
spreadsheet = gs.open_by_key(SPREADSHEET_ID)
graphsWorksheet = spreadsheet.worksheet_by_title('Graphs')
graphsWorksheet.append_table(["29/12/2020", 1, 2, 3])
return True在我的settings.py中,我像这样添加了CRONJOBS:
CRONJOBS = [
('*/1 * * * *', 'utils.cron.addToGraphs')
]使用文档中提到的命令,我似乎正确地使用:python manage.py crontab add .添加了cron
然后能够使用以下命令正确列出该列表:python manage.py crontab show
当我在本地运行它时,使用:python manage.py crontab run {taskId}
该函数确实会打印出“尝试CRON”,并将行添加到Google工作表中,耶!但只有一次。然后它就结束了。
当我使用python manage.py runserver启动python django服务器时
我没有得到打印语句,Google文档也没有更新。
我遗漏了什么?当我手动运行它时,我觉得我已经很接近它了。任何建议都很感谢,谢谢!
发布于 2020-12-20 08:03:36
相对路径不起作用,我必须将credentials.json文件的路径更新为绝对路径。我改了:credentails.json改成/Users/Matt/Documents/Project/Backend/api/credentials.json
https://stackoverflow.com/questions/65374662
复制相似问题