【发布时间】:2021-09-09 18:19:13
【问题描述】:
试图在 crontab 中安排一个 python 代码,但它不起作用。 我怎样才能理解为什么? 我已经将 cron 和 termianl 添加到全盘访问,所以这应该不是问题
当我在终端命令中运行时一切正常
python /users/myuser/slots_update.py
Crontab 命令不起作用:
45 12 * * * /usr/bin/python /users/myuser/slots_update.py
Python 脚本(里面放不同的 sql 让它更简单)
#!/usr/bin/env python
# coding: utf-8
# In[2]:
# importing the required libraries
import gspread
import pandas as pd
from oauth2client.service_account import ServiceAccountCredentials
# In[50]:
# define the scope
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
# add credentials to the account
creds = ServiceAccountCredentials.from_json_keyfile_name('key.json', scope)
# authorize the clientsheet
client = gspread.authorize(creds)
# In[51]:
# get the instance of the Spreadsheet
sheet = client.open('EGE_slots1')
# get the first sheet of the Spreadsheet
sheet_instance = sheet.get_worksheet(0)
# In[ ]:
sheet_instance.col_count
# In[52]:
sheet_instance.cell(col=1,row=1)
# In[12]:
import pandas as pd
import numpy as np
from sqlalchemy import create_engine
from datetime import datetime as dt
# In[13]:
connection = create_engine('postgresql://')
# In[47]:
slots = pd.read_sql("""
select * from teachers
""",connection)
# In[53]:
sheet_instance.update('A2',slots.values.tolist())
# In[ ]:
【问题讨论】:
-
既然你说脚本在你直接运行时有效,我相信这个问题应该在 superuser.stackexchange.com 上而不是在这里,因为这似乎是你使用 Cron 和与代码无关