Marketing Automation, Technology

Websites Can Run Scheduled Tasks with Cron

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.

Cron Diagram Explanation

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:

  1. 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.
  2. 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.
  3. I then had to login to my website via SSH. On a Mac, that took opening Terminal and typing SSH [email protected] where username was the username I wished to use and domain was the website. I was then prompted for and gave the password.
  4. 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
  5. 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.
  6. 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:

How many jobs could you automate utilizing Cron?

8 Comments

  1. 1

    Well covered article on setting up a cron, for someone new to crojobs, the most difficult part in setting up a cron is to figure out the cronjob execution interval, and it is quite commong to get a wrong interval at first try. If your cronjobs are time sensitive, it is good to include some codes in the script to echo out status so that you are kept inform of the job execution status.

  2. 2

    Hi Doug,

    A couple of things to consider when working with cron jobs.

    First, after a few dozen, you’ll wish you had a UI, a database and english-looking syntax 😉

    Second, cron will fire the job at the specified time, regardless of whether the previous invocation of the job completed. So running a job once a minute that takes 2 minutes will quickly lead to lots of the same job running.

    Next, there is next to no error reporting when something goes wrong, so you’ll need to add your own error reporting.

    I’ve addressed these in a couple of ways:
    – have the application triggered via cron look in a database to determine what needs to be run. Run it once a minute or hour depending on what you want
    – have each script create a ‘lock’ file in /tmp and if it exists, don’t start again, this prevents duplicate jobs if you don’t want them
    – if the script finds the lock file older than 1 hour (or whatever suggests you died) send an email alert
    – have the script send email on failure of the job so you know something went wrong
    – look at frameworks like Flux or commercial schedulers when your needs get beyond a few scripts

    Chris

  3. 4

    I’ll also add that on most Linux/Unix systems, “crontab -e” is what you use to edit your crontab. I think your host (Jumpline) is using a modified version for security reasons.

  4. 5

    I still remember the first day I met Cronnie. I had heard things about her, that she was dependable, always on time, but sometimes a bit confusing about her intentions.

    I found this to be true as she was a complete mystery to me at first. After asking around about her, I caught on fairly quickly as to how she liked to operate. Now, I can’t imagine a day going by without her in my life. She makes the mundane exciting, and lifts many burdens off my shoulders.

    In all seriousness, I feel like I’ve only scratched the surface with what I can automate with cron jobs. They truly are a developers best friend. If you are using someone like CPanel to manage your server, it provides a much more friendly interface to create crons. Complete with drop down menus for minute, hour, day, month, etc. that builds the cron line for you.

  5. 7

    I definitely see this is something every marketer should be using…Is there anyone who can provide this service because it sounds a bit too “techy”?

Leave a Reply