So, CRON jobs. I’ve always known about them, in fact I used to use variations of them in my first job. They’re scheduled tasks set to fire at a particular time or date, or with a particular frequency. We used these left, right and centre to fix anything that we knew would creep out of synch with everything else. From updating internal mail systems, linking to online software and importing the results to the sales database and even to link hand-held scanners to the main system. The uses for CRON jobs are endless. We use them all the time in forum software. And now I’ll show you how they work.
Apart from those examples, I’ve never used the online equivalent. Recently, Wuup’s Alan Hamlyn had a problem trying to send out an email at a particular time, and he wanted to schedule it. I grabbed him on twitter and suggested the simple task of writing a PHP script to send the mail, sticking a CRON job on it. All that took less than 140 characters, how difficult can it be? Alan suggested that he may write something on the subject but, as I was bored, and needed to learn something, I took the chance to try and figure out how they worked.
I jumped in with both feet which I feel is the best way to learn. I opened my host’s control panel and starting looking about for the icon I’d ignored for too long. At this point, it’s probably best to point out that a properly set up host will be able to give you the ability to create your own CRON job files on the server. Unfortunately the host Sypda currently uses isn’t that great. So instead, I had to use their ‘handy’ CRON job creator. And it’s rubbish.
Only in a fit of “GRRRRRR I’m a developer damn it, I can learn this!” did I continue on!
One thing I learned this time around is that the “Standard” view (the host’s attempt at making it ‘easy to use’) was a lot more complicated than the Unix view. I would strongly recommend taking the time to learn how the Unix version works, as it’s precisely 50 million times easier to use! From here on, I’ll be referring to the Unix style which is almost identical in format to the text file version, but with handy little boxes to put your variables in. Using the host is kind of handy, as there are some Unix system variables which need to be set up. When you do it this way, you don’t need to worry about so much.

OK, the breakdown (numbered as shown on the screenshot)
- The Email Addres s you place here is where any errors or confirmations will be directed to. It’s usually best to stick within the same domain as the script you are running, as you get the output much quicker.
- The Timer is the controller for when the job fires up. This bit may seem complicated, but it is easier than it appears to be. Read the section on the screenshot to give yourself a bit more of a clue.
- The Command is the reference to the script you want to run. It can be anything you wish, but there are complications to this which we will get to later on.
Here!!!
On seeing this screen I pretty much knew what I needed to do. With the email address filled out, my timer set to the value of */15 * * * * (once every 15 minutes), a script on my root to print “hello world” to the screen uploaded and the address (http://www.spydawebdesign.com/helloworld.php) was in the command box. I saved the job and waited. I tweeted about waiting. I lowered the interval to once every 5 minutes, I waited. Until I suddenly thought, “I’m a fecking eediot!”.
Why?
A file to echo something to the screen…let’s think about that a second: what screen? Where? Does the host’s server have a little screen in which my message will appear on? I doubt it; you have Twitter for that kind of thing. So what am I doing? The answer is: something very silly! A quick re-evaluation is in order. I changed my PHP file to send out a mail to myself. Simple enough script.
<?php mail(‘ivorbiggun@cox.net’,'Testing CRON’,'This is a test! If you got thi
s then it means the CRON job worked’);
?>
Before resetting the job or uploading the new file, I found that I had received an email from the server I am hosted on; I got the following:-
/bin/sh: http://www.spydawebdesign.com/helloworld.php: No such file or directory
Handy to know this before I start fiddling on. Another dawn of realisation, I’m on a server. Maybe it needs to be the address from the server root. So, I dutifully typed that in. (You think I’m gonna show you that one? Ha!) If you can’t work out your root address just make a file in the main folder of where your site is on the server with the following code and run it.
<?php
echo $_SERVER['DOCUMENT_ROOT'];
?>
REMEMBER!!!! Delete this the second you are done to stop nasty people turning your lovely site into a porn site or suchlike.
With that done, and my new mail script file uploaded I set the job back to run.
Eventually, the email arrives!!
/bin/sh: {my root directory}/spydawebdesign/helloworld.php: Permission denied
OK, well sorting that’s easy enough. FTP program, file>permissions and yeh there it is set to 644. I changed that to give Owner execute privilege, 744.
Re-ran job. Got another email:
{my root directory}/spyd
awebdesign/helloworld.php: line 1: syntax error near unexpected token `(‘
{my root directory}/spydawebdesign/helloworld.php: line 1: `’
Now, given my file only had one line, it wasn’t hard to track down the offending line. I’m glad I didn’t write an enormous script to test with.
This one had me stuck; I knew there wasn’t an error with my PHP. It was too simple to miss even the stupidest of mistakes; I knew the script had fired as I got the emails; so there was something in between the two which was stopping it. That means the server oO, no surprise there I thought. I had absolutely no clue, so I capitulated to what I hate doing…I googled it
I have a terrible trait, I love to figure stuff out myself, which as EhKho will tell you sometimes leads to me getting a touch frustrated with myself. I can’t stand the thought of using a tutorial or anything like that to help me learn. But in this case, I knew I had no choice. Yes I felt dirty and cheap, but sometimes you have to go with what works.
I found a handy little article on Sitepoint which mentioned that PHP and CRON don’t just play nice. If your server’s good enough to have PHP as an executable on it and not the Apache module, then your script just isn’t going to do anything. TYPICAL!
However, there is a handy get out clause for people on tight-arse hosts. Introducing Wget. Despite my thoughts that my hosts are incompetent, the Wget system is installed on practically every hosting site. This effectively parses the PHP through Apache. All it needs to run is a URL. So into my CRON’s command window I typed:
wget http://www.spydawebdesign.com/helloworld.php
I left everything set the same and waited…until lo and behold it worked! I got a confirmation email from the host telling me it was successfully run. I got my email generated from inside the script. It worked at last!
Please remember that a lot of this may be different on your particular host, but there should be some way to run CRON jobs, which will use the same concepts and programming. To learn about the Unix codes and the timers, you should check out the Sitepoint article about it. This goes into more depth about the timer code enabling you to fire a script at any time, day, whatever.
All you have to do now is work out what you want to automate. Do you send yourself reminders by email about important dates (if your memory is like mine), send out emails at 3am to your boss to make yourself look more productive, randomly send your mum one of a variety of stock emails telling h
er how you are and what you’ve been up to so she doesn’t worry and ring – or even worse – drop in unexpectedly to check you’re OK.
Feel free to stick any other suggestions you might have for CRON jobs below, maybe something you’ve used CRON on before, or your mistakes with CRON so I don’t feel quite as silly.




Recent Comments