Python logger file permissions












0















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: PathToTheFiletheLogFileName.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.










share|improve this question




















  • 1





    I guess you need (+w) permissions on the file, and (+x) on the parent directory.

    – Mayank Porwal
    Nov 26 '18 at 3:55
















0















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: PathToTheFiletheLogFileName.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.










share|improve this question




















  • 1





    I guess you need (+w) permissions on the file, and (+x) on the parent directory.

    – Mayank Porwal
    Nov 26 '18 at 3:55














0












0








0


1






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: PathToTheFiletheLogFileName.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.










share|improve this question
















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: PathToTheFiletheLogFileName.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.







python logging






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 4:25









eyllanesc

84k103562




84k103562










asked Nov 26 '18 at 3:51









DougDoug

888




888








  • 1





    I guess you need (+w) permissions on the file, and (+x) on the parent directory.

    – Mayank Porwal
    Nov 26 '18 at 3:55














  • 1





    I guess you need (+w) permissions on the file, and (+x) on the parent directory.

    – Mayank Porwal
    Nov 26 '18 at 3:55








1




1





I guess you need (+w) permissions on the file, and (+x) on the parent directory.

– Mayank Porwal
Nov 26 '18 at 3:55





I guess you need (+w) permissions on the file, and (+x) on the parent directory.

– Mayank Porwal
Nov 26 '18 at 3:55












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer
























  • Thanks, solved. It was the permissions on the parent folder. I changed these with chmod 777 DirName directly from terminal rather than with python (I had tried that yesterday and I guess for the same reasons that too had a permissions error).

    – Doug
    Nov 27 '18 at 3:26











  • Glad it worked out. Cheers mate!

    – Sagar Dawda
    Nov 27 '18 at 7:42











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474579%2fpython-logger-file-permissions%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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.






share|improve this answer
























  • Thanks, solved. It was the permissions on the parent folder. I changed these with chmod 777 DirName directly from terminal rather than with python (I had tried that yesterday and I guess for the same reasons that too had a permissions error).

    – Doug
    Nov 27 '18 at 3:26











  • Glad it worked out. Cheers mate!

    – Sagar Dawda
    Nov 27 '18 at 7:42
















1














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.






share|improve this answer
























  • Thanks, solved. It was the permissions on the parent folder. I changed these with chmod 777 DirName directly from terminal rather than with python (I had tried that yesterday and I guess for the same reasons that too had a permissions error).

    – Doug
    Nov 27 '18 at 3:26











  • Glad it worked out. Cheers mate!

    – Sagar Dawda
    Nov 27 '18 at 7:42














1












1








1







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 4:55









Sagar DawdaSagar Dawda

455211




455211













  • Thanks, solved. It was the permissions on the parent folder. I changed these with chmod 777 DirName directly from terminal rather than with python (I had tried that yesterday and I guess for the same reasons that too had a permissions error).

    – Doug
    Nov 27 '18 at 3:26











  • Glad it worked out. Cheers mate!

    – Sagar Dawda
    Nov 27 '18 at 7:42



















  • Thanks, solved. It was the permissions on the parent folder. I changed these with chmod 777 DirName directly from terminal rather than with python (I had tried that yesterday and I guess for the same reasons that too had a permissions error).

    – Doug
    Nov 27 '18 at 3:26











  • Glad it worked out. Cheers mate!

    – Sagar Dawda
    Nov 27 '18 at 7:42

















Thanks, solved. It was the permissions on the parent folder. I changed these with chmod 777 DirName directly from terminal rather than with python (I had tried that yesterday and I guess for the same reasons that too had a permissions error).

– Doug
Nov 27 '18 at 3:26





Thanks, solved. It was the permissions on the parent folder. I changed these with chmod 777 DirName directly from terminal rather than with python (I had tried that yesterday and I guess for the same reasons that too had a permissions error).

– Doug
Nov 27 '18 at 3:26













Glad it worked out. Cheers mate!

– Sagar Dawda
Nov 27 '18 at 7:42





Glad it worked out. Cheers mate!

– Sagar Dawda
Nov 27 '18 at 7:42




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474579%2fpython-logger-file-permissions%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin