Greg's Blog

helping me remember what I figure out

Cron Jobs and PHP

| Comments

I have been wanting to schedule a few tasks on my hosted site that call PHP scripts and “do stuff” for ages, but just never found the time to look into it. Finally the opportunity arose to look into it. As with all things, I started by scouring the Internet here is the resources I came across that purely refers to PHP and the command line:

  1. http://www.phpfreaks.com/phpmanual/page/features.commandline.html

So the idea was all along to write a bunch of scripts that would be executed say on a daily basis using the cron job. Now my initial impression was that you could only call scripts from the command line. However then I also read the CPanel documentation in a little more depth and realised that I can call php pages via the GET command… So I went ahead and wrote this little script that would send a message out to me (which is one of the examples from the PHP documentation for using the mail() function).

<?php
$myname = “My name”;
$myemail = “my@emailaddress.com”;

$contactname = “The person to be contacted”;
$contactemail = “contact@emailaddress.com”;

$message = “hello from happy me”;
$subject = “A mail not refused by Hotmail”;

$headers .= “MIME-Version: 1.0\r\n”;
$headers .= “Content-type: text/html; charset=iso-8859-1\r\n”;
$headers .= “From: “.$myname.” <".$myemail.">\r\n”;
$headers .= “To: “.$contactname.” <".$contactemail.">\r\n”;
$headers .= “Reply-To: “.$myname.” <$myreplyemail>\r\n”;
$headers .= “X-Priority: 1\r\n”;
$headers .= “X-MSMail-Priority: High\r\n”;
$headers .= “X-Mailer: Just My Server”;

mail($contactemail, $subject, $message, $headers);
?>

By calling the url I tested that the page worked properly and in due time I received a message. So now it was time get the CRON job up and running. To this end I entered the following command into the “command to run” field of CPanel: GET http://www.teacupinastorm.com/clients/sample_mail.php. I set an interval of five minutes for the job, so in theory I would receive a message every five minutes. I submitted it and all that was left for me to do was wait patiently for a message to wing it’s way into my inbox. While I was waiting I did a little more research which is listed below. Having said that this solution worked a treat and the message landed in my inbox!! Hurrah!

The documentation for setting up Cron jobs in CPanel can be found here: http://www.cpanel.net/docs/cp/cronJobs.htm

And RedHat has some additional information on CRON jobs here: http://www.redhat.com/support/resources/tips/cron/cron.html

And below is an alternative suggestion for PHP and Cron:

http://www.htmlcenter.com/tutorials/tutorials.cfm/155/PHP/
This tutorial makes use of PHP and Lynx to call a page (if you are using PHP as an Apache module) and the cron command looks something like this:
* * * * * lynx -dump http://www.somedomain.com/cron.php