1

how can i create new file in /var/log directory using python language in OSX leopard? i tried to do it using os.open function but i get "permission denied"

thanks in advance

nur
  • 161
  • 2
  • 11

3 Answers3

6

Only root can write in /var/log/ on Mac OS X...:

$ ls -ld /var/log
drwxr-xr-x  60 root  wheel  2040 Oct  6 17:00 /var/log

Maybe consider using the syslog module in the standard library...

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • @nur, `import syslog` of course -- see http://docs.python.org/library/syslog.html for all the details! – Alex Martelli Oct 07 '09 at 05:25
  • @alex thank u very much. actually what i want to achieve is that, i am writing a launchagent application for osx using python. it writes some meta info in a sqlite db from time to time. these data cannot be seen by user without root privilege. so i need to keep this db file in place where only root has access. how can i achieve that? – nur Oct 07 '09 at 05:34
  • @nur, you can achieve that only by running as root (not recommended, but it IS the only way to do exactly what you ask: update a sqlite db which ONLY root can access). – Alex Martelli Oct 07 '09 at 05:45
1

It probably failed because /var/log has user set to root and group set to wheel. Try running your python code as root and it will probably work.

Asaph
  • 159,146
  • 25
  • 197
  • 199
  • thank u. my application is a launchagent application. it runs during start up and saves some data in file time to time. data saved by the application cannot be viewed without root privilege. how can i achieve that? – nur Oct 07 '09 at 05:17
1

You can create the log file as root and then change the owner to the user your script is run as

# touch /var/log/mylogfile
# chown myuser /var/log/mylogfile

where mylogfile is your logfile and myuser is the user the script will be run as

also look into logrotate

John La Rooy
  • 295,403
  • 53
  • 369
  • 502