Jump to content

Archived

This topic is now archived and is closed to further replies.

Tjk

PHP script to be run by CRON

Recommended Posts

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

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

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

Help appreciated as always.
-Tjk

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Set the cronjob to write to file instead. That will tell you if the problem is with mail config or cronjobs.

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

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:

[code]#!/usr/bin/php -q[/code]

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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.