Araz Jafaripur
Araz Jafaripur

Reputation: 906

Permission in creating folder by cronjob and maniuplate it in website side

I have a file which running with cronjob and creating some file and folder, but when enter into that folder in website mode I can't create files or anything in that folder.
I think the folders created by cronjob owner is root, But when want to access to it in website mode, we are in apache owner and can't manipulate it.
When creating folder with 0777 permission it's work fine. Anyone knows a solution for this problem by without changing folder permission from 0775 to 0777 ? thanks.

Upvotes: 0

Views: 1570

Answers (3)

Skarpi Steinthorsson
Skarpi Steinthorsson

Reputation: 521

This can be solved by either running the cronjob as the Apache owner or setting the folder you are creating the file in to propagate the group ownership to all created files/folders.

To run a cronjob as another user:

crontab -eu apache-user

This will run the job as the same user that is running Apache so there will be no need to change the permissions. This is not ideal if the file should not be writable front the website.

Obviously you need to change "apache-user" to the user that is running Apache. Example: www-data on Ubuntu, apache on CentOS.

To set the folder to inherit group ownership so all files created in the folder will be accessible by the group:

chmod g+s folder-you-are-writing-to/

And make sure the Apache group is set as the group:

chgrp apache-user folder-you-are-writing-to/

Upvotes: 1

Starfire1337
Starfire1337

Reputation: 51

You can change the owner of the folder you make in the cron job to whatever your web user is.

chown -R apache:apache /path/to/folder

Edit: If you are to do this, there is always a security risk of having the web user being able to create files etc.

Upvotes: 2

mariobgr
mariobgr

Reputation: 2201

If your php script creating the folder is run by root, then you can exec() chown command to change folder owner to apache

chown -R username:group /directory

Upvotes: 1

Related Questions