Ubuntu server 16.04.5 LTS
I have a python script which creates log files using the following code:
today = datetime.today()
datem = datetime(today.year, today.month, today.day)
logger = logging.getLogger('processImport')
hdlr = logging.FileHandler('{0}myLog_{1}-{2}-{3}.log'.format(myLogFileLocation, datem.year, datem.month, datem.day))
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
saving a log entry with:
logger.info(logMessage)
I then have a cron job that deletes older log files after a certain period by executing a python file which uses os.remove(fullFilePath)
to delete the file.
However, I am getting a permissions error when this cron job executes.
OSError: [Errno 13] Permission denied: PathToTheFile\theLogFileName.log
When I check the permissions for the file they are set to:
-rw-r--r-- 1 www-data www-data etc etc
What do I need to do to enable the cron job to have permission to delete the log files please?
Thank you.
There seems to be a write issue permission with the folder. Changing the permission should help.
Try this:
log_dir = '/abs/path/of/directory'
os.chmod(log_dir, 0777)
Let me know how it goes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With