1

I have a python script called backupapp.py

import subprocess, time
from datetime import date

app_name = 'xxxxx'

capture_backup = subprocess.check_output(['heroku','pg:backups:capture','--app',app_name]) # Make a new backup 

time.sleep(5)

info_backup = subprocess.check_output(['heroku','pg:backups:info','--app',app_name])

name_backup = info_backup[11:15] # Extract the name of the backup database

today = date.today().strftime("%m_%d_%Y")
filename = name_backup + '_' + today + '.dump'

url = subprocess.check_output(['heroku','pg:backups:url', name_backup,'--app',app_name])

url = url[:-1] # Remove \n from the string

print "Beginning Download....."

download_backup = subprocess.check_output(['curl', '-o', filename, url])

This script works when I run it in the terminal python backupapp.py

However, I want to run this everyday at noon.

So I used crontab -e and added this:

0 12 * * * cd /Users/myuser/Desktop/Work/appbackup && python backupapp.py

But this isn't running. I checked for some answers online to see the logs, but the logs do not exist.

7
  • 1
    Did you try with absolute path to python? Commented Nov 20, 2019 at 13:13
  • You mean instead of using cd, just directly python /Users/myuser/Desktop/Work/appbackup/backupapp.py? Commented Nov 20, 2019 at 13:30
  • No, I mean /path/to/python /Users/myuser/Desktop/Work/appbackup/backupapp.py or cd /Users/myuser/Desktop/Work/appbackup && /path/to/python backupapp.py Commented Nov 20, 2019 at 13:32
  • If I may make a suggestion (I'm assuming linux) I've pretty much entirely switched from cron to systemd timer files. Slightly more work to set up, much easier to manage and troubleshoot. YMMV. Commented Nov 20, 2019 at 13:33
  • @JaredSmith, based on the path IMO this is MacOS Commented Nov 20, 2019 at 13:34

1 Answer 1

1
0 12 * * * root cd /Users/myuser/Desktop/Work/appbackup && ./backupapp.py

Make sure your python file has a valid shebang at the top with the appropriate path to your python binary like

#!/usr/local/bin/python3
Sign up to request clarification or add additional context in comments.

8 Comments

This will work only on specific case! And why do you root, in path you will see the username.
The username in the path is just that, the path to the script that is attempting to be run. root here is instructing crontab what user will execute the script. I added root here, but it could be something else I suppose. Critically important, however, will be that the OP has their shebang in order.
This (again) will work in specific case. And may not work in MacOS. And in MacOS this is the username (myuser in this case)
I see what you're saying. That's correct, the /Users/... does look more like MacOS than other OS's...excellent catch!
Thanks! Should I change root to myuser in this case? I am assuming that I also have to make the .py file executable with chmod +x? A heads up, if I run this in the terminal -- cd /Users/myuser/Desktop/Work/appbackup && ./backupapp.py, it works.
|

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.