2

I'm trying to run a PHP script, like writing something in a txt file. If I manually load the file it works, but not with a cron job. I have cPanel and I followed what I read in the help page, but nothing works.

I tried this command

php -q /user/public_html/folder/cronTest.php

but it didn't work. I also tried

/user/local/bin/php -q /cronTest.php

as it says here.

I also tried to put #!/user/public_html/bin/php -q at the top of the file, but that didn't work either. I've also tried to change file permissions with similar results.

I really need to learn how to use cronjobs, cause I'll have a lot of work where it is needed.

I intend to use cPanel to set corncobs, so I hope the problem is with the command and/or the in code in the PHP file.

Conspicuous Compiler
  • 6,403
  • 1
  • 40
  • 52
Pedro Soares
  • 623
  • 2
  • 10
  • 15

2 Answers2

1

Have you tried something like this?

php -q /home/your_username/public_html/folder/cronTest.php

Also, if you can't find a solution, I'd suggest you to contact your hosting provider as they might be able to help you faster, or even fix a misconfiguration that might be causing the problem.

EDIT: Got it to work by using:

/usr/bin/php -q /home/your_username/public_html/path/to/script.php
Telmo Marques
  • 5,066
  • 1
  • 24
  • 34
  • Thank you Tom for the advice, but i allready contact them, but they help in things related with the server, etc...they say i need to fnd help near programares, cause the help team is not for this kind of problems. And because is my first time with cronjobs...i googled a lot for this subject, but i could't solve it. Thanx. – Pedro Soares Dec 24 '11 at 00:05
  • Is there any `error_log` file in the same directory as the script you're trying to run? It might have some useful information. Also, give `/usr/local/bin/php -q /cronTest.php` a go, as suggested by Conspicuous Compiler. `/usr/local/bin/php` it's the path were the binary is usually at. – Telmo Marques Dec 24 '11 at 00:07
  • Sorry, in my previous comment I meant `/usr/local/bin/php -q`, as the path to the PHP file may differ because of server configuration. Try it together with the several file path's you already tried. – Telmo Marques Dec 24 '11 at 00:20
  • I realy have a problem here! `/usr/local/bin/php -q /home2/user_name/public_html/consulta24/cronTest.php` Now i'm using this, should i replace "local" for "home2/user_name/peblic_html"? I also tryied just `/usr/local/bin/php -q /consulta24/cronTest.php`, but nothing! – Pedro Soares Dec 26 '11 at 11:43
  • Just try to replace `/home2/` with `/home/` and `user_name` with your actual username (the username you use on cPanel's login), like so: `/usr/local/bin/php -q /home/your_actual_username_here/public_html/consulta24/cronTest.php`. Just for reference, the `/usr/local/bin/php` path is an absolute path beginning in the root directory (`/`) and points to the php executable. So nothing needs to be changed in this path, the only thing you need to change in the example I give above is your username. – Telmo Marques Dec 26 '11 at 15:59
  • Thanx @TomS, but is not working. I was already using my username, and now i replaced `/home2/` with `/home/` as you said, but...not working. I realy don't know what can be the problem. – Pedro Soares Dec 26 '11 at 20:00
  • I have great news!! It's working and the way you said! Well almost cause i had to change `/usr/local/bin...` to `usr/bin...` and when i saw it was working. Thank you very much :) – Pedro Soares Dec 26 '11 at 23:23
  • Happy to hear that :) I edited my answer to document the working version of the command, as it might be useful to someone else. – Telmo Marques Dec 27 '11 at 00:45
1

We use cron to run nightly tasks in a php facebook game. We do it by using curl like this:

/usr/bin/curl http://www.ourdomain.com/page.php

If I remember right we had some issues using localhost to try to avoid external lookups. Also we tried using php command line execution, which mostly worked but caused a few strange bugs.

Ravenex
  • 621
  • 3
  • 10