简体   繁体   English

通过 Cron 运行 PHP 脚本

[英]Running PHP script via Cron

I'm codding a php script, using Instagram Private PHP Api.我正在编写一个 php 脚本,使用 Instagram Private PHP Api。

It's work fine via SSH under "root" user, but when I try to run it via browser or cron, I getting error: Warning: chmod(): Operation not permitted in .....它在“root”用户下通过 SSH 工作正常,但是当我尝试通过浏览器或 cron 运行它时,出现错误:警告:chmod():操作不允许在 .....

I guess that something wrong with permissions, but I am not really good in server administration and can't understand what I can do =(我想权限有问题,但我在服务器管理方面并不是很擅长,无法理解我能做什么=(

Please help, how I can fix this problem?请帮忙,我该如何解决这个问题?

The user that PHP runs as must have permissions to chmod the given file or directory.运行 PHP 的用户必须具有 chmod 给定文件或目录的权限。 If you're running this script via CRON, you get to set the user that PHP runs as right in the CRON job.如果您通过 CRON 运行此脚本,则可以在 CRON 作业中将 PHP 运行的用户设置为正确的用户。 If you're visiting the script in a browser, PHP is likely running as php or php-fpm or the web server user.如果您在浏览器中访问该脚本,则 PHP 很可能以 php 或 php-fpm 或 Web 服务器用户身份运行。

Simply ensure that the given file or folder is owned by the user that PHP runs as.只需确保给定的文件或文件夹归 PHP 运行的用户所有。

Note: It is not recommended that you run this script as root in CRON.注意:建议您在 CRON 中以 root 身份运行此脚本。

Because Apache (or the web server you're using) executes PHP using different Linux user (usually www-data ), which obviously have different permission than the user account you used in access via SSH.因为 Apache(或您使用的 Web 服务器)使用不同的 Linux 用户(通常是www-data )执行 PHP,这显然与您通过 SSH 访问时使用的用户帐户具有不同的权限。

To tackle the problem, you first have to know the folder / file you're going to chmod() belongs to who.要解决这个问题,您首先必须知道您要chmod()的文件夹/文件属于谁。 If it belongs to root , then it's not suggested to chmod via any scripts that is accessible by public due to security concerns.如果它属于root ,那么出于安全考虑,不建议通过任何公共可访问的脚本来chmod

If it belongs to your user name, say foo , you can change the ownership of the folder / file you're going to chmod() to be accessible by www-data group using chown() in SSH console, then you chmod() command can be executed without problem.如果它属于您的用户名,例如foo ,您可以更改您要chmod()的文件夹/文件的所有权,以便www-data组在 SSH 控制台中使用chown()访问,然后您chmod()命令可以毫无问题地执行。

If you are editing /etc/crontab , make sure the user parameter (the one after week) is root .如果您正在编辑/etc/crontab ,请确保用户参数(一周后的)是root

If you are editing crontab via crontab -e , add user parameter crontab -eu root .如果您通过crontab -e编辑 crontab,请添加用户参数crontab -eu root

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM