Weather Script Fixed

Dec 26, 2004

The PHP weather script that I use in the sidebar is now fixed. Something broke it and so it looked kind of strange for a week. At first I wasn’t sure what the problem was. I thought it might’ve had something to do with the fact that my host had just upgraded the version of PHP used (to 4.3.10). If not that then possibly the National Weather Service changed something in the way they put out the information. After debugging the whole script I found the problem and it should have been obvious. The NWS Weather page, which hasn’t changed much for a while, had something new on it and it was right at the top of the page. A link was added for a WML page for cell phones. This threw the array that stored the weather information out of order. A slight adjustment and now I once again know what to wear when I go outside.

Using Cron with PHP

Dec 20, 2004

The other day I was trying get Cron to work with PHP and after a lot of tinkering it worked. I am jotting the steps I used down hear so I don’t forget how I did it and for anyone else who might be interested. Cron is a Unix command that lets you schedule a process to take place at a specified interval. This is really handy if you have some process that you just want to happen every so often automatically, say like backing up a database.

For starters you’ll need to upload your PHP script to your server. I should mention that your script doesn’t have to necessarily be PHP, it can be Perl or another server side scripting languages. This script will be executed automatically by the Cron process you set up.

The next step is to set up a text file with instructions for Cron. I called mine cron.txt. The basic syntax of the Cron instruction file should look something like this:

0,10,20,30,40,50 * * * * /home/directory/www/example.php

The first 5 fields are where you specify how often the process should run. Here is what each of the 5 fields specify:

First field = minutes (0,10,20,30,40,50 used for this example)
Second field = hours of the day (* used for this example)
Third field = days of the month (* used for this example)
Fourth field = months of the year (* used for this example)
Fifth field = days of the week (* used for this example)

If there is a * that means all, i.e. run all days of the week or all hours of the day. In my example I used multiple values for the minutes. I basically told it to run every 10 minutes. The second part of the instructons is the path to where your php script is. After the instructions are set, upload your cron.txt file. I put it in the same directory as my php script.

The next step is to login to the shell of your server and enter a Cron command at the Unix prompt like this:

crontab /home/directory/www/cron.txt

That basically saves your Cron instructions file. To verify you did this right just enter the following command:

crontab -l

That will list all current cron jobs.

At this point if you did every thing right you should be done.
Unfortunately, the first time around for me it wouldn’t work. To make sure the problem wasn’t with my PHP script, I loaded it up in a web browser to make sure it performs the way it was supposed too. It did, so I knew it was something else. After trying a lot of different approaches, I started to think that the problem was that PHP is meant to be parsed by a web server and that is usually triggered by a web browser. I was getting a lot of “permission denied” errors and I just couldn’t get the script to run through the Cron process. Finally I modified my cron.txt file as follows and it worked:

0,10,20,30,40,50 * * * * /usr/local/bin/php -q /home/directory/www/example.php >> /dev/null

The server’s PHP executable location had to be specified in the cron.txt instruction file . This is the “/usr/local/bin/php -q” part. The -q part means run in quiet mode (i.e. don’t output any HTML headers). The >>.dev/null part means throwaway any output. PHP used normally will provide output to a web browser, but my script just needed to execute. There would be no browser interaction. One of the things I was stuck on the longest was that I had placed the PHP executable location path in the PHP script, but apparently my server didn’t like it there.

Winter in the Whites

Dec 14, 2004

Cabot Cabin on Mt. Cabot
Winter is here in the White Mountains of NH. Above is a picture of Cabot Cabin from a recent hike. The cabin is a little below the summit of Mt. Cabot(4170′).

MySQL String Replace

Dec 10, 2004

While messing around with a PHP script for string replacement (using the str_replace function) in a MySQL database field, I came across a way to find and replace the string by using only MySQL. Maybe it’s just my ignorance of MySQL’s capabilities, but I didn’t think there would be anyway to do this without the use of PHP or any other scripting language. In this case I was trying to remove a domain name from an entire column for the purpose of converting absolute URLs to relative URLs. Anyway, I thought I would post it here for future reference.

If you run the following query:

UPDATE table_name SET column_name = replace(column_name,’’, ”)WHERE column_name LIKE ‘%’;

It will take URLs like this:

And change them to:


Of course it can be done with any kind of string that appears in many fields, not just URLs.

Links: 12-07-04

Dec 7, 2004