I have this test code that just saves an XML file to a folder. I wanted to schedule a crontab job to have it running every minute, but I can only get permission to save the XML to disk when I use sudo and type my password. When I schedule it with crontab it prints the string but doesn’t save the file. How could I have it working?
#!/usr/bin/python
import sys
import time
from socket import *
from xml.dom.minidom import Document
def main(args):
doc = Document()
with open("/Users/lucasp/Desktop/LogsXML" + time.strftime("%Y-%m-%d-%H.%M.%S", time.localtime()) + ".xml", "w") as f:
f.write("doc")
string = "File saved! : LogsXML/" + time.strftime("%Y-%m-%d-%H.%M.%S", time.localtime()) + ".xml"
print string
if __name__ == '__main__':
sys.exit (main (sys.argv))
Fix the permissions before you run the script: Find out the uid cron runs with (probably “crontab”). Use chown (probably as root) to change the gid of the file
LogsXMLto that group, and grant group write access to the file. E.g.,(If the script needs to create new files, chown the directory and grant write and execute access with
chown g+wx <directory>).