Run a PHP file in a cron job using CPanel

PhpCronCpanel

Php Problem Overview


I am just trying to run a PHP script using a cron job within CPanel - is this the correct syntax:

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

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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

Solution 3 - Php

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

Solution 4 - Php

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

Solution 5 - Php

This is the way:

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

Solution 6 - Php

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

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

Solution 7 - Php

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

Solution 8 - 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.

Solution 9 - Php

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

Solution 10 - Php

Suggested By Experts.

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

Solution 11 - Php

It is actually very simple,

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

Solution 12 - Php

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.

Solution 13 - Php

On a Hostgator CPANEL this worked for me:

php /home/here_your_user_name/public_html/cronJob.php

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZabsView Question on Stackoverflow
Solution 1 - PhpPankView Answer on Stackoverflow
Solution 2 - PhpSomy AView Answer on Stackoverflow
Solution 3 - Phpk102View Answer on Stackoverflow
Solution 4 - PhpEmmanuelView Answer on Stackoverflow
Solution 5 - PhpAsiView Answer on Stackoverflow
Solution 6 - PhpMike VolmarView Answer on Stackoverflow
Solution 7 - PhpBenKennishView Answer on Stackoverflow
Solution 8 - PhpLakin MohapatraView Answer on Stackoverflow
Solution 9 - PhpSajjadView Answer on Stackoverflow
Solution 10 - PhpAli AkramView Answer on Stackoverflow
Solution 11 - PhpDushyanth KandiahView Answer on Stackoverflow
Solution 12 - PhpAnjana SilvaView Answer on Stackoverflow
Solution 13 - PhpNicoliView Answer on Stackoverflow