【问题标题】:Schedule Python script with crontab doesn't work使用 crontab 安排 Python 脚本不起作用
【发布时间】: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 和与代码无关

标签: python cron


【解决方案1】:

使用 json 文件的完整路径。

creds = ServiceAccountCredentials.from_json_keyfile_name('key.json', scope) --> creds = ServiceAccountCredentials.from_json_keyfile_name('/a/b/c/key.json', scope)

【讨论】:

  • 感谢您的回答!但不幸的是,没有帮助(当然我改变了 cron 的时间来检查它:))
  • @SmirnovaAnna 这肯定是个问题。你可能有另一个问题..
猜你喜欢
  • 2021-05-25
  • 1970-01-01
  • 2013-01-12
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
  • 1970-01-01
  • 2017-07-01
  • 2015-08-18
相关资源
最近更新 更多