A cron is useful for scheduling certain types of task on the website so that they can be done on a regular basis without having to manually do it or reminded to do it.
Here are some examples on how crons can be used on an website:
1. Every 1st day of the month, delete all temporary files on the web server that are not being used.
2. Archive or back up files and/or database once a week.
3. Email the website admin with the website's statistics at the end of the day.
As you can see from these examples, you can easily see the value of a cron. It basically automates the work that you normally would have to do manually if you didn't have the cron capability.
My website has a cron that gets executed every 5 minutes. You might be wondering, what is so important that I need to do it every 5 minutes on my website?
Well, it's no other than my custom built tweets.
I basically send an email with a picture(s) attachment to a specific email address and my cron checks that email's INBOX every 5 minutes to see if there are any new emails. If a new email is found then the cron will retrieve the email and post it to the tweet section of my website with the picture attachment. This is great since I can send emails with pictures from my iPhone virtually from anywhere!
Although there's potentially 5 minute delays in posting, it's so much faster and easier than trying to post something from a browser on the iPhone.
Therefore, I wrote a PHP script that checks a specific email INBOX then retrieves the emails, saves the attachment files, then insert them into a MySQL database table, which is equivalent to posting a blog on my website.
Once I had my PHP script written and manually tested it, I had to find a way to schedule it. Fortunately, I have cPanel as the control panel of my hosting and cPanel includes Cron jobs.
From the Cron jobs page in the cPanel, all you have to do is specify how often you want it to be executed then type "php [full path to the php file to be executed]" in the Command text box.
Once you click on the "Add New Cron Job" button, this is how it would look like:
The scheduling option is very flexible. For example, the sample schedule cron in the screenshot above indicates that cron should execute the mycron.php file located on /home/mywebiste/www/ directory at 9:45am on 1st and 15th day of every other month.
By default, the output of each cron execution is sent to the email address identified on the Cron Jobs page. I would recommend that you schedule the cron to run every minute first so that you can receive the cron output email immediately (every minute). If your php script has an error, then the email will include the error message. Your scheduled php script should have "Successful!" or "Completed without error" echo output at the end so that they would be included in the email, which is a clear indicator that the cron executed the script without any issue.
Obviously, you can have it output any other useful information, such as the number of records or files processed, etc.
Once you have confirmed the successful email from cron, you can then schedule it to be executed on your desired frequency.
If you do not wish to get email confirmation from cron, you can append the
>/dev/null 2>&1at the end of the command, which I did since I do not want to receive email from my tweet cron every 5 minutes
Please note that the most common mistake made when setting up the cron is specifying the incorrect path to the php file. If you are not sure what the full path to the file is then include the following line in any php file and it will show you the full path:
One last thing to remember is that you must set the file permission to be executable on the PHP file. I usually set it to 755.