Cron job is not working properly in cpanel
Asked Answered
U

5

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)

Umont answered 23/10, 2012 at 12:50 Comment(11)
Do you have some relative paths in php code that you want to run via cron, maybe some includes?Philipps
can you post your complete command from cron job ?Whitefaced
Try running PHP file using Console (like putty) and check there are any errors. Problem could be on include() or require() file paths.Sweepback
@DušanRadojević yes i do have included some filesUmont
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/nullWhitefaced
@Whitefaced can you please give me some examples, how to achieve cronjob via http with curl? should change cronjob path or path of included files?Umont
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)Whitefaced
@Whitefaced 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 ?Umont
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/nullWhitefaced
thanks @Whitefaced it works as you said, please post is as answer so i can accept itUmont
Then anyone with the URL can run your cron - using curl is a terrible idea.Librium
W
16

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!

Whitefaced answered 1/11, 2012 at 17:49 Comment(2)
Finally ! This is the only working answer I found, thanks a lot, great ideaMarney
It may not work with addon domains though (use absolute path to make it work there).Gourmandise
I
1

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

lynx -dump http://localhost/script.php
Inconsequential answered 23/10, 2012 at 13:36 Comment(0)
T
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

Theophany answered 2/7, 2020 at 11:42 Comment(0)
V
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.

Vigorous answered 23/10, 2012 at 13:38 Comment(0)
F
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

False answered 4/7, 2020 at 8:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.