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

我只是尝试在 CPanel 中使用 cron 作业运行一个 PHP 脚本——这是正确的语法吗:

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

我没有收到任何电子邮件通知,说明 cron 已经完成,我需要做什么特定的 PHP 文件?

237487 次浏览

>/dev/null stops cron from sending mails.

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

In crontab system :

  • /usr/bin/php is php binary path (different in some systems ex: freebsd /usr/local/bin/php, linux: /usr/bin/php)
  • /home/username/public_html/cron/cron.php should be your php script path
  • /dev/null should be cron output , ex: /home/username/stdoutx.txt

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

I used this command to activate cron job for this.

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

on godaddy server, and its working fine.

This is the way:

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

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

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:

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:

#!/usr/bin/env php

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

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

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.

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

/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

The following two commands also work fine but do not send email:

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

php -f /home/Same As Above

Suggested By Experts.

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

For domain specific Multi PHP Cron Job, do like this,

/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.

Hope this helps someone.

It is actually very simple,

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

On a Hostgator CPANEL this worked for me:

php /home/here_your_user_name/public_html/cronJob.php