0

I set up a cron job for writing every minute from a python script an entry into a log file which is created from the python script. The python script is working fine if I execute it manually but it gets not executed automatically from running the cron job.

Python script:

import logging
import os

dir_path = os.path.dirname(os.path.realpath(__file__))
filename = os.path.join(dir_path, 'test_log.log')

# Logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

file_handler = logging.FileHandler(filename)
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
logger.addHandler(file_handler)

def do_logging():
    logger.info("test")


if __name__ == '__main__':
    do_logging()

I added Terminal and cron to full disc access. The cron job was successfully created I am able to list the cron job with crontab -l. What could be the reason?

Cron view:

enter image description here

Full disk access:enter image description here

1
  • Is it using python2 or python3?
    – nohillside
    Apr 20, 2022 at 13:54

1 Answer 1

2

I logged my output of the cron job as following:

* * * * * python /Users/maxhager/test/some.py > /Users/maxhager/test/backup.log 2>&1

> /Users/maxhager/test/backup.log 2>&1 is creating a file called backup.log and logs standard output and errors.

From this information I saw that my python was not found so I changed python to python3 and it worked.

Solution:

* * * * * python3 /Users/maxhager/test/some.py > /Users/maxhager/test/backup.log 2>&1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .