Just a Girl Weblog

Remembering Who I Really Am

Cron and PHP

I recently designed a website for a client which inluded access to an MLS Data Feed (IDX/XML)—a service provided for members of the Realtors Association of Maui. One the items on her wish list was an early‑morning email summarizing the previous day's new MLS listings.

Although cron is commonly used to automate system maintenance or administration, you can utilize cron to send an email with information from your site.


Cron is a scheduling daemon available on Linux and other Unix variants that executes commands at specified dates and times according to instructions in a set of files called "crontabs." A system‑wide crontab file is usually stored in the /etc directory, and each users crontab file in the /var/spool/cron directory.

Creating Your Crontab With Plesk

Most web hosting providers allow access to cron through an easy‑to‑use web interface. Mine is Plesk (version 9.5.4). To add a cron job in Plesk:

  1. Log in to Plesk.
  2. Select the domain you wish to manage, and choose Scheduled Tasks from the Additional Tools group.
  3. Select the system user account and click Schedule New Task.
  4. Specify when to run your command, and which command to run.

For example, if you want to run your php script daily at 7:00am and have the information (new listings) sent to you via email, specifiy the following in the input boxes:

cron - New Task Details
cron - New Task Details

Note: If you are running PHP‑CGI (not as an Apache module), you may need to suppress HTTP header output by adding the Quiet‑mode switch to the command:

  • php ‑q httpdocs/newListingsEmail.php

By default, the server will send output (or error messages) from the cron job to the email address you specified when you set up the domain. You can change this email address in 'Preferences.'

cron - New Task
cron - New Task

The PHP Script

  • <?php
  • require_once('ext/inc_funcGen.php');
  • require_once('ext/inc_funcXML.php');
  • require_once('ext/cls_phpmailer-lite.php');
  • require_once('int/inc_constants.php');
  • require_once('int/inc_email.php');
  • require_once('int/inc_ram.php');
  • $yesterday = date('Y-m-d', strtotime('-1 day', APP_NOW));
  • $list = getNew($yesterday);
  • $xml = getSearchXML($list, $method);
  • $listingsHTML = displayEmail($xml, $yesterday);
  • $mail = new PHPMailerLite();
  • $mail->Subject = \'New MLS Listings: \' . $yesterday;
  • $mail->IsHTML(true);
  • $mail->SetFrom(LIST_EMAIL, DISPLAY_NAME);
  • foreach($nladdr as $addr)
  • {
  • $mail->AddAddress($addr);
  • $mail->Body = $listingsHTML;
  • $mail->Send();
  • $mail->ClearAddresses();
  • }
  • echo 'Email sent to subscribers of \'New MLS Listings\': ' . DATE_ISO;
  • ?>

Potential PHP Gotchas

I have a 'library' of common include files that reside in a directory outside the domains on my server:

Server File System

A list of directories where the require(), include(), fopen(), file(), readfile() and file_get_contents() functions look for files is specified in each domain's vhost.conf file.

  • <IfModule mod_php5.c>
  • ...
  • php_admin_value open_basedir
  • "/var/www/vhosts/
  • httpdocs:/var/www/vhosts/library:/tmp"
  • php_value include_path
  • "/var/www/vhosts/library/ext:."
  • ...
  • </IfModule>

Interestingly, these directives are ignored when executing a PHP script through cron, and I received the following email message when I tested the cron job:

Solution: Hard-linking Files
Using find to Confirm Creation of Hard Links

Where to Go for More Information

Filed under: , , , , ,