I'm new to setting up cron jobs and I have a very simple task to run a Python script every minute. When I run:
crontab -e
I have:
*/1 * * * * cd path/to/python/file && $(which python3) test.py >> cron_log.txt 2>&1
test.py looks like this:
"""
Test File for CRON Job
"""
import datetime as dt
import os
import subprocess
FILE_PATH = os.path.dirname(os.path.abspath(__file__))
def main():
"""Run Stuff"""
with open(os.path.join(FILE_PATH, 'test.txt'), 'a') as file:
file.write(str(dt.datetime.now()))
file.write('\n')
process = subprocess.run(['git', 'status'],
stdout=subprocess.PIPE,
universal_newlines=True
)
process = subprocess.run(['git', 'pull'],
stdout=subprocess.PIPE,
universal_newlines=True
)
process = subprocess.run(['git', 'add', '.'],
stdout=subprocess.PIPE,
universal_newlines=True
)
process = subprocess.run(['git', 'commit', '-m', '\'Adding Log File\''],
stdout=subprocess.PIPE,
universal_newlines=True
)
process = subprocess.run(['git', 'push'],
stdout=subprocess.PIPE,
universal_newlines=True
)
if __name__ == "__main__":
main()
It basically just writes the current datetime to a file and then adds it to a git repository (the Python file is located inside a git repository I've cloned locally).
When I test this script manually (just copying and pasting what I have in crontab -e
) it works. But after waiting a few minutes, I don't see any changes so I think my crontab isn't running. Any help?
Also I've made sure my Python file is executable:
8 -rwxrwxrwx 1 vincela staff 976 Dec 29 22:01 test.py
8 -rw-r--r-- 1 vincela staff 1124 Dec 29 21:47 cron_log.txt
8 -rw-r--r-- 1 vincela staff 270 Dec 29 21:47 test.txt