简体   繁体   English

使用 CPanel 在 cron 作业中运行 PHP 文件

[英]Run a PHP file in a cron job using CPanel

I am just trying to run a PHP script using a cron job within CPanel - is this the correct syntax:我只是想在 CPanel 中使用 cron 作业运行 PHP 脚本 - 这是正确的语法:

/usr/bin/php -q /home/username/public_html/cron/cron.php >/dev/null

I am not getting any email notifications stating a cron has been completed, do I need to do anything specific with the PHP file?我没有收到任何说明 cron 已完成的电子邮件通知,我是否需要对 PHP 文件执行任何特定操作?

I used this command to activate cron job for this.我使用此命令为此激活 cron 作业。

/usr/bin/php -q /home/username/public_html/yourfilename.php

on godaddy server, and its working fine.在godaddy 服务器上,它工作正常。

In crontab system :在 crontab 系统中:

  • /usr/bin/php is php binary path (different in some systems ex: freebsd /usr/local/bin/php , linux: /usr/bin/php ) /usr/bin/php是 php 二进制路径(在某些系统中不同,例如:freebsd /usr/local/bin/php ,linux: /usr/bin/php
  • /home/username/public_html/cron/cron.php should be your php script path /home/username/public_html/cron/cron.php应该是你的 php 脚本路径
  • /dev/null should be cron output , ex: /home/username/stdoutx.txt /dev/null应该是 cron 输出,例如: /home/username/stdoutx.txt

So you can monitor your cron by viewing cron output /home/username/stdoutx.txt所以你可以通过查看 cron 输出/home/username/stdoutx.txt来监控你的 cron

>/dev/null stops cron from sending mails. >/dev/null阻止 cron 发送邮件。

actually to my mind it's better to make php script itself to care about it's logging rather than just outputting something to cron实际上在我看来,最好让php脚本本身来关心它的日志记录,而不是仅仅输出一些东西到 cron

This is the easiest way:这是最简单的方法:

php -f /home/your_username/public_html/script.php

And if you want to log the script output to a file, add this to the end of the command:如果要将脚本输出记录到文件中,请将其添加到命令的末尾:

>> /home/your_username/logs/someFile.txt 2>&1

这是方法:

/usr/bin/php -q /home/username/public_html/yourfilename.php >/dev/null

这条 cron 线在使用 cpanel 的 hostgator VPS 上为我工作。

/usr/bin/php -q /home/username/public_html/scriptname.php

I've had problems using /usr/bin/php on CPanel as it is compiled as a "cgi-fcgi" binary and not "cli".我在 CPanel 上使用 /usr/bin/php 时遇到问题,因为它被编译为“cgi-fcgi”二进制文件而不是“cli”。 Try using /usr/local/bin/php or, as it is first in the path anyway, just use 'php' instead:尝试使用 /usr/local/bin/php 或者,因为它是路径中的第一个,只需使用'php':

php /path/to/script.php

If you want to run the script as an executable, give it +x perms and use the following as the first line of the script:如果要将脚本作为可执行文件运行,请给它 +x perms 并使用以下内容作为脚本的第一行:

#!/usr/bin/env php

I hope your problem is with path & php binary as well.我希望您的问题也与路径和 php 二进制文件有关。 If you have fixed the path as per older answers, please use php-cli instead of php command while running cron job.如果您已按照旧答案修复了路径,请在运行 cron 作业时使用 php-cli 而不是 php 命令。

It may be possible php_sapi_name() is not returning cli . php_sapi_name()可能没有返回cli Its returning something else like cgi-fcgi etc.它返回其他东西,如cgi-fcgi等。

/usr/bin/php-cli -q /home/username/public_html/cron/cron.php >/dev/null

I hope it will help.我希望它会有所帮助。

This works fine and also sends email:这工作正常并且还发送电子邮件:

/usr/bin/php /home/xxYourUserNamexx/public_html/xxYourFolderxx/xxcronfile.php /usr/bin/php /home/xxYourUserNamexx/public_html/xxYourFolderxx/xxcronfile.php

The following two commands also work fine but do not send email:以下两个命令也可以正常工作,但不发送电子邮件:

/usr/bin/php -f /home/Same As Above /usr/bin/php -f /home/同上

php -f /home/Same As Above php -f /home/同上

专家推荐。

/usr/local/bin/php /home/username/public_html/path/to/cron/script

其实很简单,

php -q /home/username/public_html/cron/cron.php

For domain specific Multi PHP Cron Job , do like this,对于特定于域的Multi PHP Cron Job ,请这样做,

/usr/local/bin/ea-php56 /home/username/domain_path/path/to/cron/script

In the above example, replace “ea-php56” with the PHP version assigned to the domain you wish to use.在上面的示例中,将“ea-php56”替换为分配给您要使用的域的 PHP 版本。

Hope this helps someone.希望这可以帮助某人。

在 Hostgator CPANEL 上,这对我有用:

php /home/here_your_user_name/public_html/cronJob.php

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

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