8

I have setup cron job, it is working properly, however when i directly runs the php file(from the path/url) it successfully runs, but from cron its not. However i set another very simple file to make sure my cron command/path is set correctly i created another file and used simple php mail function and its is running successfully. Kindly suggest me the possible areas to look into it. (I am using Cpanel)

Manoj
  • 477
  • 5
  • 8
  • 19
  • 1
    Do you have some relative paths in php code that you want to run via cron, maybe some includes? – Develoger Oct 23 '12 at 12:52
  • can you post your complete command from cron job ? – alan978 Oct 23 '12 at 12:53
  • Try running PHP file using Console (like putty) and check there are any errors. Problem could be on include() or require() file paths. – Muthu Kumaran Oct 23 '12 at 12:54
  • @DušanRadojević yes i do have included some files – Manoj Oct 23 '12 at 12:57
  • if you have relative paths as Dusan mentioned that it is best way to call your script in cronjob via http with curl. This should solve your problem. curl http ://www.example.com/phpscript.php > /dev/null – alan978 Oct 23 '12 at 12:59
  • @alan978 can you please give me some examples, how to achieve cronjob via http with curl? should change cronjob path or path of included files? – Manoj Oct 23 '12 at 13:04
  • instead of putting in cronjob command like php -f /path_to_script/script.php, put command like this "curl http: //domain.com/yourscript.php" (without double quotes) – alan978 Oct 23 '12 at 13:10
  • @alan978 so my current path is `/usr/local/php4/bin/php /home/serverusername/public_html/test_cron/readmails.php` should i change it to `curl http://mydomain.com/test_cron/readmails.php` ? – Manoj Oct 23 '12 at 13:14
  • Yes, and if you don't want output than you can add > /dev/null at the end. So complete command should be: curl http: //mydomain.com/test_cron/readmails.php > /dev/null – alan978 Oct 23 '12 at 13:18
  • thanks @alan978 it works as you said, please post is as answer so i can accept it – Manoj Nov 01 '12 at 13:55
  • 2
    Then anyone with the URL can run your cron - using curl is a terrible idea. – Ashley Oct 28 '14 at 13:55

5 Answers5

15

Instead of putting in cronjob command like

php -f /path_to_script/script.php 

put command like this:

curl http://domain.com/yourscript.php

if you want to suppress output you can add > /dev/null at the end.

So full command would be:

curl http://domain.com/yourscript.php > /dev/null

Hope this helps!

alan978
  • 535
  • 3
  • 6
1

As alternative to cURL, you can call your script through text-based browser. Something like this:

lynx -dump http://localhost/script.php
a.ilic
  • 354
  • 3
  • 11
1

I have faced the same problem. I am using task scheduling in laravel project on cpanel. My command was

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

It is a version issue. My server default php version is 7.1.33 but in my project the php version is 7.2. So, when i run the schedule command it takes the php7.1 which is not compatible with my projecct. So, according to cpanel documentaions the command is actually for different php version

/usr/local/bin/ea-php99 /home/shadhinapp/domain_path/path/to/cron/script.

Any my cron job command is:

/usr/local/bin/ea-php72 /home/user_name/path_to_your_project/artisan schedule:run >> /dev/null 2>&1

Ziaur Rahman
  • 1,148
  • 11
  • 24
0

You shouldn't have any relative paths -- this includes both files and commands. For example, you shouldn't call just cp if you want to copy a file, but something like /bin/cp with the full path to the command. To find out the full path, you can run which <cmd> at the command line.

TAH
  • 1,658
  • 1
  • 19
  • 37
0
curl http://mydomain/auto_push/task.php?p=1

This is live working code from my project. See the image below:

enter image description here

XpressGeek
  • 2,889
  • 3
  • 23
  • 27