Jump to content


Photo

PHP script to be run by CRON


  • Please log in to reply
7 replies to this topic

#1 Tjk

Tjk
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 14 August 2006 - 08:11 PM

I've got CRON set up to run this file below every minute... (this is the entire contents of the file)

<?php
$to= "myemail@emailprovider.com";
$subject= "cron test";
$message= "if you receive this cron is working!";
mail($to, $subject, $message);
?>

Am I missing something about running PHP scripts using CRON or is it a server configuration problem?

Help appreciated as always.
-Tjk


#2 Prismatic

Prismatic
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego

Posted 14 August 2006 - 08:21 PM

What's the problem? Not getting the email?

#3 Tjk

Tjk
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 14 August 2006 - 09:02 PM

Yeah. The code itself will work when embedded in HTML code but when trying to use CRON to load the pure PHP code itself to mail it to me it doesn't work.

#4 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 14 August 2006 - 09:04 PM

Include some error checking then execute your file from the command line so you can see the output.  Cron will not return anything, so you can't tell what the error may be.  Executing the file from the command line is the same as cron doing it.

#5 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 14 August 2006 - 09:07 PM

Set the cronjob to write to file instead. That will tell you if the problem is with mail config or cronjobs.

Example:
<?php
setlocale(LC_TIME, "NL_nl");
$time = strftime("%H:%M:%S");
$fp = fopen('test.txt','w');
fwrite($fp,'time: '.$time."\n");
?>

Check file afterwards. If everything is as expected, evaluate your mail settings.

#6 Tjk

Tjk
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 15 August 2006 - 06:29 AM

Out of interest, if I load the script through my web browser should I see a blank page or should I get the error I'm getting which says "Warning: fopen(test.txt): failed to open stream: Permission denied"?

Also, I tried it with CRON with no luck.

#7 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 15 August 2006 - 06:37 AM

You need to create a file test.txt
I am a PHP Guru, ask me questions if you want to

#8 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 15 August 2006 - 06:49 AM

since CRON jobs are not run from a browser, the server doesn't always know what to use to parse the code.  try adding this:

#!/usr/bin/php -q

to the top of your script (before the opening PHP tag); this line tells the server (linux i believe for this line) what to use to parse the code and where it's located.  i don't often use command line, so someone can probably correct my terminology, but this is what i need to add to my CRONs usually.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users