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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7332804/
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-26 02:29:51  来源:igfitidea点击:

Run a PHP file in a cron job using CPanel

phpcroncpanel

提问by Zabs

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 文件执行任何特定操作?

采纳答案by Somy A

In crontab system :

在 crontab 系统中:

  • /usr/bin/phpis php binary path (different in some systems ex: freebsd /usr/local/bin/php, linux: /usr/bin/php)
  • /home/username/public_html/cron/cron.phpshould be your php script path
  • /dev/nullshould be cron output , ex: /home/username/stdoutx.txt
  • /usr/bin/php是php程序二进制文件路径(在某些系统的前夫不一样:在FreeBSD /usr/local/bin/php,Linux的:/usr/bin/php
  • /home/username/public_html/cron/cron.php应该是你的php脚本路径
  • /dev/null应该是 cron 输出,例如: /home/username/stdoutx.txt

So you can monitor your cron by viewing cron output /home/username/stdoutx.txt

所以你可以通过查看 cron 输出来监控你的 cron /home/username/stdoutx.txt

回答by Pank

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 服务器上,它工作正常。

回答by k102

>/dev/nullstops cron from sending mails.

>/dev/null阻止 cron 发送邮件。

actually to my mind it's better to make phpscript itself to care about it's logging rather than just outputting something to cron

实际上在我看来,最好让php脚本本身来关心它的日志记录,而不是仅仅向 cron 输出一些东西

回答by Emmanuel

This is the easiest way:

这是最简单的方法:

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

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

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

回答by Asi

This is the way:

这是方法:

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

回答by Mike Volmar

This cron line worked for me on hostgator VPS using cpanel.

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

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

回答by BenKennish

I've had problems using /usr/bin/php on CPanel as it is compiled as a "cgi-fcgi" binary and not "cli". Try using /usr/local/bin/php or, as it is first in the path anyway, just use 'php' instead:

我在 CPanel 上使用 /usr/bin/php 时遇到问题,因为它被编译为“cgi-fcgi”二进制文件而不是“cli”。尝试使用 /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

回答by Lakin Mohapatra

I hope your problem is with path & php binary as well. If you have fixed the path as per older answers, please use php-cli instead of php command while running cron job.

我希望您的问题也与路径和 php 二进制文件有关。如果您已按照旧答案修复了路径,请在运行 cron 作业时使用 php-cli 而不是 php 命令。

It may be possible php_sapi_name()is not returning cli. Its returning something else like cgi-fcgietc.

有可能php_sapi_name()是不回来了cli。它返回其他类似的东西cgi-fcgi

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

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

I hope it will help.

我希望它会有所帮助。

回答by Sajjad

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/同上

回答by Ali Akram

Suggested By Experts.

专家推荐。

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