php Cron 作业和文件夹权限 - 权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10598530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:34:09  来源:igfitidea点击:

Cron job and folders permissions - permission denied

phppermissionscron

提问by JonoB

I have a folder above the webroot that is used to temporarily store user files generated by a php web application. The files may, for example, be PDF's that are going to be attached to emails.

我在 webroot 上方有一个文件夹,用于临时存储由 php web 应用程序生成的用户文件。例如,这些文件可能是要附加到电子邮件中的 PDF。

The folder permissions are set to rwxr-xr-x (0755). When executing a procedure from the web application, the files get written to this folder without any issues.

文件夹权限设置为 rwxr-xr-x (0755)。从 Web 应用程序执行过程时,文件会毫无问题地写入此文件夹。

I have now also set up a cron job that calls the php script to execute that exact same procedure as above. However, the PDF cannot be saved into the above folder due to failed permissions - the cron job reports back a permission deniederror.

我现在还设置了一个 cron 作业,它调用 php 脚本来执行与上面完全相同的过程。但是,由于权限失败,PDF 无法保存到上述文件夹中 - cron 作业报告permission denied错误。

I have tried setting the folder permissions to 0775 and still get a permission denied. However, when the permissions are 0777, then the cron job then works fine.

我已尝试将文件夹权限设置为 0775,但仍然被拒绝权限。但是,当权限为 0777 时,cron 作业就可以正常工作。

This seems very strange to me - why does the cron get a permission denied at 0755 but it works fine through the web app?

这对我来说似乎很奇怪 - 为什么 cron 在 0755 获得权限被拒绝,但它通过网络应用程序正常工作?

回答by Zlatko

The probable answer is that the cron job executes under your user - and the directory is owned by apache (or www-data or nobody or whatever user your web server runs as).

可能的答案是 cron 作业在您的用户下执行 - 该目录归 apache 所有(或 www-data 或 nobody 或您的 Web 服务器运行的任何用户)。

To get it to work, you could set up the cron job to run as the web server user. Something like this:

要使其工作,您可以将 cron 作业设置为以 Web 服务器用户身份运行。像这样的东西:

su -l www-data -c 'crontab -e'

Alternatively, you could change the permissions to 775 (read-write-execute for the owner and group, and read-execute for others) and set the group ownership of the folder to the user running the cron job.

或者,您可以将权限更改为 775(所有者和组的读写执行,其他人的读取执行)并将文件夹的组所有权设置为运行 cron 作业的用户。

However, you have to make sure that if you're deleting something or descending into folder which is created by apache, you could still run into problems (apache would create a file which it itself owns, and your user cannot delete it then, regardless of the directory permissions.

但是,您必须确保如果您要删除某些内容或进入由 apache 创建的文件夹,您仍然可能会遇到问题(apache 会创建一个它自己拥有的文件,并且您的用户无法删除它,无论如何的目录权限。

You could also look at some stuff like suphp or whatever is up to date - where the web server processes are ran under your username, depending on your system architecture.

您还可以查看诸如 suphp 之类的东西或任何最新的东西 - Web 服务器进程在您的用户名下运行,具体取决于您的系统架构。

回答by Karoly Horvath

Permission are given to user-group-everybody. That's what the 3 characters denote.

权限被授予用户组每个人。这就是3个字符所表示的。

Your php script runs as a different user&group than the cron job, so they observe different permissions.

您的 php 脚本作为与 cron 作业不同的用户和组运行,因此它们观察到不同的权限。

Check chownand chgrp, or try to run the cron job with the same user.

检查chownchgrp,或尝试使用同一用户运行 cron 作业。

回答by Kh?i

It depends on which user you have defined the cronjob.

这取决于您定义了 cronjob 的用户。

If you're root (not recommended) it should work. If you're the web-user (e.g. www-data on ubuntu) it should work as well.

如果您是根用户(不推荐),它应该可以工作。如果您是网络用户(例如 ubuntu 上的 www-data),它也应该可以正常工作。

sudo su - www-data
crontab -e

回答by luis

if you are using cpanel to run a php, you can try something like this: "php /home/algo/public_html/testcron.php" ... just write: php (the rute of the script)/yourscritpt.php"

如果您使用 cpanel 来运行 php,您可以尝试这样的操作:"php /home/algo/public_html/testcron.php" ... 只写:php (the rute of the script)/yourscritpt.php"