0

Trying to schedule a python code in crontab, but it doesn't work. How Can I understand the reason why? I've already add cron and termianl to Full disk access, so this shouldn't be a problem

everything works fine when I run in terminal command python /users/myuser/slots_update.py

Crontab command which doesnt'work: 45 12 * * * /usr/bin/python /users/myuser/slots_update.py

Python script (put different sql inside to make it simplier)

#!/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[ ]:

1
  • Since you say the script works when you run it directly, I believe this question should go on superuser.stackexchange.com rather than here, as it seems to be a problem specific to your use of Cron and not code-related Commented Sep 9, 2021 at 10:18

1 Answer 1

1

Use full path to the json file.

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for answer! But unfortunately, doesn't help (of course I changed time in cron to check it now :) )
@SmirnovaAnna This is a problem for sure. You may have another problem..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.