1

I'm running an hourly cron job for testing. This job runs a python file called "rotateLogs". Cron can't use extensions, so the first line of the file is #!/usr/bin/python. This python file(fileA) then calls another python file(fileB) elsewhere in the computer. fileB logs out to a log file with the time stamp, etc. However, when fileB is run through fileA as a cron job, it creates its log files as rw-r--r-- files.

The problem is that if I then try to log to the files from fileB, they can't write to it unless you run them with sudo permissions. So I am looking for some way to deal with this. Ideally, it would be nice to simply make the files as rw-rw-r-- files, but I don't know how to do that with cron. Thank you for any help.

EDIT: rotateLogs(intentionally not .py):

#!/usr/bin/python
#rotateLogs
#Calls the rotateLog function in the Communote scripts folder
#Designed to be run as a daily log rotation cron job
import sys,logging
sys.path.append('/home/graeme/Communote/scripts')
import localLogging
localLogging.localLog("Hourly log",logging.error)
print "Hello"

There is no command in crontab, but it is running properly on the hourly cron(at 17 minutes past the hour).

FileB's relevant function:

def localLog(strToLog,severityLevel):
    #Allows other scripts to log easily
    #Takes their log request and appends it to the log file
    logging.basicConfig(filename=logDirPath+getFileName(currDate),format="%(asctime)s %(message)s")
    #Logs strToLog, such as logging.warning(strToLog)
    severityLevel(strToLog)
    return

I'm not sure how to find the user/group of the cronjob, but it's simply in /etc/cron.hourly, which I think is root?

WarSame
  • 502
  • 4
  • 20
  • I appreciate the comment but this didn't work. I set it both in /etc/profile and in ~/.bashrc, to 002 but it still is making the file as a 644. – WarSame Nov 01 '15 at 23:30
  • I think if you could the following to the question then it might be easier to answer: 1) the crontab line; 2) the code in `rotateLogs`(.py) calling the `fileB`, 3) the code in `fileB` creating/appending the log file; and possibly 4) the user/group/shell of the cronjob (and the user/group of logfile, if different). It might be simple, such as using `#!/usr/bin/env python`, or adding `chmod g+w logfile` somewhere, or [`os.umask(002)`](http://stackoverflow.com/questions/10291131/how-to-use-os-umask-in-python). – Kenney Nov 01 '15 at 23:55
  • Good call, I'll update those now. – WarSame Nov 01 '15 at 23:57
  • Updated. /env python doesn't change anything. I'll try os.umask next. Thanks for all these ideas, one's probably going to work. – WarSame Nov 02 '15 at 00:11
  • *"Cron can't use extensions"* - what does that mean? *Unix* doesn't use file extensions. – dimo414 Nov 02 '15 at 00:35
  • Instead of rotateLogs.py it must be rotateLogs. I didn't realize all of Unix did that, so thanks for the info. – WarSame Nov 02 '15 at 00:41
  • Kenney, it ended up working. I used python's os.umask, which I'll update in the question. If you post your suggestion as an answer, I'll accept it for you. It's bad form for me to answer and accept myself, right? EDIT: Forgot to say, thanks a lot! I was pretty without direction, but your pointers sorted me out. – WarSame Nov 02 '15 at 02:43

1 Answers1

3

It turns out that cron does not source any shell profiles (/etc/profile, ~/.bashrc), so the umask has to be set in the script that is being called by cron.

When using user-level crontabs (crontab -e), the umask can be simply set as follows:

0 * * * * umask 002; /path/to/script

This will work even if it is a python script, as the default value of os.umask inherits from the shell's umask.

However, placing a python script in /etc/cron.hourly etc., there is no way to set the umask except in the python script itself:

import os
os.umask(002)
Kenney
  • 8,687
  • 12
  • 21