Q: What is a cron job?
A: A cron job is an automated task that runs on a linux server, similar to Scheduled Tasks on windows.
The main idea is that you can run a process without you or someone else manually setting it off – my first experience with cron jobs was when working with an online auction website. As you can imagine, an auction runs for a fixed time period. So lets say an auction runs for 7 days. What happens at the end of 7 days? The database says this auction is closed. If the auction did not sell, whether the reserve was not met, or their were no bids at all, then we want to automatically relist that auction.
There are two options:
a) run a process when a page load occurs… sucks because the page load is gunna take longer due to the extra processes occurring
b) run a cron job every x minutes, looking for auctions that have passed their end time, and either relist them or close them down permanently
I tend to code in PHP and MySQL, which means that I create a PHP file that does all the maintenance tasks required and I run it at set intervals. The cool thing is, if you want to be running different tasks at multiple times of the day/week/year, just setup multiple cron jobs.
Thats great, but how the hell do I setup a cron job?
I’m glad you asked!
If you are comfortable working with command line, then you might want to visit a site like this. If, like me, you have access to cPanel, life is much, much easier.
How to setup a cronjob in cPanel
1. login to cPanel
2. click on the cron jobs tab (near the bottom)
3. Select your time you want the script to run. The options are pretty straightforward. If you select Minutes > 30 that means the script will run at xx:30am/pm. You can also hold the Ctrl key to select multiple minutes/hours etc.
4. Enter the “command to run”. For my purposes, I generally want to load a php file. It doesn’t hurt to put these files outside of the public web folder “just in case”. Something like this should do the trick on a standard cPanel/WHM server:
/usr/local/bin/php -f /home/myusername/scripts/ascript.php
The first part is the “path to php“. You might want to ask your hosting providers what this is, if the above doesn’t work. The second part is the absolute path to your file. By default on a cPanel server the structure is /home/your_account_username/ where you can modify everything inside your account folder. Generally contained in the account username folder is a www/public_html folder with your live web files.
Hope this is of some help – cron jobs are a breeze – once they are running as desired its fun dreaming up new ways to make them more and more sophisticated… after all, now the server is actively taking away the daily grind!
If you have any questions do not hesitate to ask below.
Hi Steve,
Thanks for sharing about cron jobs.
I need your help..I have a problem with my auction web, and cron job cant work properly. Do you know what the causes?
I you dont mind, please to reply to my mail.
Thank you sooo muuuch..