We have a number of redundant monitoring systems at work that regularly execute processes. Some run every minute, some once a night depending on what they are doing. For example, we might execute a script that exports all customers who haven’t made a purchase in 30 days to send them a coupon.
Rather than trying to keep track of all of these by hand, it’s much easier to build jobs that are automatically scheduled and executed. On Unix-based systems, this is accomplished with Cron. For you folks that know what you’re doing, feel free to educate me and the readers if I throw out any disinformation.
It’s unfortunate, but the typical web developer isn’t acquainted with Cron at all. Even if they are, web hosting companies often don’t supply access to, or support of, Cron. My host is one of the latter – they allow you to use it, but they don’t support it.
What is Cron?
Cron is named for the Greek word Chronos, meaning time. Cron runs in a continuous loop to run tasks that are accumulated by Crontab (perhaps named for tabulator. Those tasks are typically referred to as Cronjobs, and can reference scripts in your site.
How do I setup the Crontab
Getting Cron to actually run can be challenging, so here’s what I learned and how I did it for If Suck:
- I set up my script to check Twitter’s API to see if anyone had replied to @ifsuck. I compared those messages to the messages I already saved on the website, entering any new ones.
- Once the script was working, I enabled permissions for the User to execute the script (744) and added the script reference to my Cronjob file – more on that later.
- I then had to login to my website via SSH. On a Mac, that took opening Terminal and typing SSH email@example.com where username was the username I wished to use and domain was the website. I was then prompted for and gave the password.
- I then attempted to run the script directly from the command prompt by typing the filename and relative path on the server: /var/www/html/myscript.php
- Once I got it working correctly, I added the necessary Unix code in the first line of the file: #!/usr/bin/php -q . I believe this simply tells Unix to utilize PHP to execute the script.
- At the Terminal command line, I typed crontab (others may need to type crontab -e) and hit enter… and that was all that was needed!
Syntax for your Cronjob File
With regard to #2 above, Cron utilizes an ingenious scheme for determining when your scripts will be executed. In fact, you can actually copy and paste this into your Cronfile (on my host, it’s located in /var/spool/cron/ with the filename the same as my username).
# +—————- minute (0 – 59)
# | +————- hour (0 – 23)
# | | +———- day of month (1 – 31)
# | | | +——- month (1 – 12)
# | | | | +—- day of week (0 – 6) (Sunday=0 or 7)
# | | | | |
* * * * * /var/www/html/myscript.php
The above will execute my script every minute. If I only wanted it to run once an hour, I would just put how many minutes after the hour that I wish it to run, so if it was at the 30 minute mark:
30 * * * * /var/www/html/myscript.php
Be sure you set the permissions to this file as executable, too! I found that syntax, permissions, and executing crontab from the Terminal window were the most important factors. Each time I’d resave the file, I’d find my permissions needing reset as well!
UPDATE: If you’d like to ensure the jobs are running, one simply way is to update a database field with the last time the script was run. If it’s more infrequent, you may just script an email sent to yourself.
Additional Cron Resources:
- Unix Daemon – Dean Wilson
How many jobs could you automate utilizing Cron?
We're entering a new industrial era, fueled by the Internet of Everything, cloud and social media. It's also an unprecedented opportunity for CIOs to lead the change, creating business value and capturing new opportunities.