0

I am running a python CGI script which must create a video at a directory location.

Platform : Ubuntu 14.04 64-bit

Python Version : 2.7.6

The script is being run by the user : www-data

I have a directory mydir. The permissions to mydir are 777 (recursive). This directory is owned by user: www-data and group www-data. The Python script creates a new directory in a sub-directory of mydir, but there is an Exception raised:

IOError: [Errno 13] Permission denied: 'path/to/file.ext'

I observed that the new directory which was created, surprisingly had the following state:

drwxrwxr-x 3 me   me   4096 <Timestamp> newdir

When I checked earlier the process was being run by www-data but now the folder is owned by the current user I am logged into. (indicated by 'me' here). Moreover all permissions are set except check the others permissions r-x. NO WRITE PERMISSIONS. That justifies the exception raised.

I tried to do a os.umask(0000) as well as os.chmod('mydir',0777). First one did not have any effect. Second one, I don't have permissions somehow. Can somebody please help. This issue is real irritating. What am I missing here?

Note: The python file is triggered as follows: A cronjob calls a PHP file which after some processing then sends a post request to the Python script.

EDIT: Please let me know if the solution by @MattDMo works (I won't be testing it as I have changed my approach). I will mark it as accepted if somebody finds it working.

psiyumm
  • 6,437
  • 3
  • 29
  • 50
  • I already have searched around. But not found a solution – psiyumm May 12 '14 at 17:12
  • Is the `cron` job being run by `www-data`? – MattDMo May 12 '14 at 17:18
  • Are you using curl or php in the cron job, that can change the user that is running the entire jobset. – thebwt May 12 '14 at 17:26
  • @MattDMo No, `cron` job is not run by `www-data`. I observed that if I set `cron` job from the current logged in user, folder is owned by that user. I also ran `cron` job using `root`, then folder was owned by `root`. – psiyumm May 13 '14 at 06:35
  • @thebwt I am using `php` to call the file in `cron` job. I see the owner changing, because when I write the log file, it is owned by `www-data` but new folder is owned by the user who created cron job `me` or `root`. – psiyumm May 13 '14 at 06:39

1 Answers1

1

It looks like all you need to do is edit your crontab and add www-data as the user:

*/5 * * * * www-data php call_python.php

This can also be done from the command line by running crontab with the -u option, which specifies the name of the user whose crontab is to be changed:

sudo crontab -u www-data -e
MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • Thank you! I actually changed my approach. I cut off the `CGI` part from my system. I did some refactoring. Now I am directly calling the python script, ported the `PHP` operations to the `Python` itself. – psiyumm May 13 '14 at 16:34
  • I will not be marking the above answer as accepted as I have not tested it. If somebody finds this to work, please let me know. – psiyumm May 13 '14 at 16:35