0

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
KazikM
  • 1,257
  • 5
  • 21
  • 29
Vincent
  • 7,808
  • 13
  • 49
  • 63
  • What's in `cron_log.txt`? Have you tried logging to it to see what's running? Tried `pdb`? – Jim J Dec 30 '20 at 05:14
  • cron_log.txt has the output of after I git push. There are no errors in it. However, cron_log.txt is only created because I manually ran it myself. It doesn't seem to run automatically with cron. Also, what's pdb? – Vincent Dec 30 '20 at 05:18
  • `pdb` (for "Python DeBugger") is a good tool for interactively stepping through code as it executes, either from the first line, or starting from a breakpoint. https://www.digitalocean.com/community/tutorials/how-to-use-the-python-debugger has a good intro. – Jim J Dec 30 '20 at 05:24
  • Oh sorry, yes, I am very familiar with `pdb` for Python. I thought it was a reference to something in setting up CRON jobs. The python script has no problem. I've run it manually and it runs producing the `test.txt` output. It seems that it's CRON that's not running. – Vincent Dec 30 '20 at 05:28
  • And the contents of `test.txt` never changes? `cron_log.txt` never changes? Have you tried writing or throwing an exception from inside your file operations on `test.txt`, to see if your code get there? – Jim J Dec 30 '20 at 05:29
  • I do believe it's something related specific to my Mac. I have access to a different linux environment, and when I set it up there, it works. Every minute the current datetime is appended to test.txt as expected. – Vincent Dec 30 '20 at 05:29
  • On Mac, cron_log.txt and test.txt never changes. – Vincent Dec 30 '20 at 05:29
  • For example, https://stackoverflow.com/questions/59123499/crontab-is-not-running-local-bin-script-catalina-bigsur seems to be a similar question, but I've tried the fixes there and it doesn't seem to work. – Vincent Dec 30 '20 at 05:30
  • Which version of macOS are you using? I'm on Catalina, so I can only test that locally. – Jim J Dec 30 '20 at 06:21
  • Mac OS Catalina 10.15.5 – Vincent Dec 30 '20 at 08:23
  • You can try to replace `*/1 * * * *` with `* * * * *` which means the same - "at every minute". – KazikM Dec 31 '20 at 16:24

0 Answers0