Cron Jobs - The Good, The Bad, the Ugly

Ranked #76,745 in Computers & Electronics, #1,178,608 overall

Setting Up Cron Jobs Can Be Painful or Joyful...Which Do You Want?

Cron Jobs are powerful tools to help you schedule tasks that you'd like to automate on a regular basis.  But, at the same time, the Linux cron service has a deeply cryptic syntax that feels more like ancient Greek than anything.  This article will help you decipher the Greek and decide if you want to configure cron, or use another online cron service with simpler configuration.

How To Setup Cron Jobs

Setup of Cron Jobs has come a long way in 30 years. This module will show you the old way, the new way, and give you the tools to decide which works best for you.
  1. Why Use Cron Jobs?
  2. Cron Jobs: Old School
  3. Cron Jobs, 21st Century
  4. Recommended Online Cron Services
Important!

Why Use Cron Jobs?

The term 'cron' comes from UNIX (http://en.wikipedia.org/wiki/Cron). 'Cron' is short for chronograph, a measure of time. You create cron jobs to run a task (like a script) automatically at a certain time. On Windows, these are called Scheduled Tasks. Cron jobs are very useful to backup databases, update blogs and RSS feeds, cleanup file systems, etc.

Cron Jobs: Old School

"The hard way"

Cron jobs are done in Linux/UNIX using the crontab file.

Reading it (and writing a entry) feels a little like transcribing an ancient spell. The file is a list of entries. Each entry represents a time to execute and a command to run. The time to execute may be one time, or repeating.

This is an example crontab entry:

1 0 * * * echo -n "" > /www/apache/logs/error_log

Here's what those bits all mean:

1 2 3 4 5 /sbin/ifconfig
1 represents the minute (0 - 59) to execute. So 1 = :01 of the hour.
2 represents the hour (0 - 23) to execute, So 2 = 2:00 am here
3 represents the day of the month, so 3 here is the 3rd.
4 represents the month of th year, so 4 here is April
5 represents the day of the week, starting at 0. 5 is Friday

Putting it all together, we have:
"Run /sbin/ifconfig on Friday, April 3rd @ 2:01am"

An asterisk (*) means that the field can have any value. Our first example:

1 0 * * * echo -n "" > /www/apache/logs/error_log

clears the Apache log at 00:01 every day, every month. This can get ugly in a hurry:

*/5 * * * * /home/user/test.pl

Runs test.pl every 5 minutes.

*/5 * * * 1-5 /home/user/test.pl

Runs test.pl every 5 minutes, Monday-Friday only. Ugh. Imagine a really complex one:

00 09-18 * * 1-5 /home/user/check-db-status

Runs check-db-status at the top of the hour, from 9am to 6pm, every Monday-Friday.

Will this work? Yes. Is it easy to remember? No, not even close. crontab is a very powerful tool and there circumstances where you must use it:

  • You need to run a script on a local machine

  • You need to run a script that shouldn't be accessed from the internet (e.g. administrator scripts)

  • You don't have access to the internet to use an online cron service


These are all great examples when crontab is necessary.

Cron Jobs, 21st Century

The Easy Way

Well, thankfully in the 21st Century, we can finally get away from this archaic 1979 syntax designation that a handful of people can repeat off the top of their head. Web developers today may or may not have access to cron jobs directly in their account.

An online cron service does exactly what a local cron job does, but with a twist--they execute a PHP, ASP, JSP or other CGI web script that is accessible on the internet at your scheduled time. You can run these just like you would a local script, but there are a few advantages:

  • Whatever you can do in a complex PHP script, you can now run as a cron

  • You can pass parameters to the same script to alter behavior at different times (e.g. a DB backup script that does incremental log backups nightly, and full backups once per week, using different parameters)

  • Online cron services can have a simpler interface to configure jobs (more intuitive)

  • For free web hosts that don't offer cron jobs locally, this is a great alternative to get crons cheaply or for free

  • Some online cron services offer additional features like site monitoring, so you get two tools for the price of one



Online Cron Services are now available in many flavors--free (limiting you on the number of jobs you can run, or how often they execute), or paid services (ranging from $5 to $50+ per year). Some have additional services like site monitoring, some are just cron execution engines.

Many are built on the phpJobScheduler package, a popular PHP framework to run crons. The main downside to this framework is that it expects you to understand and input the cron syntax discussed above. That defeats the point of making things easier in most cases.

Recommended Online Cron Services

(Free Cron Jobs and Paid Cron Jobs)

A list of known and recommended Cron Job services
Cronless
English site. Free and Premium service. Premium offers unlimited jobs and site monitoring. Very reliable, easy to use. Paid model is $x per year of access.
CronJob Services (German)
German site, offers paid and free services. Seems to have uptime monitoring too.
CronDroid
English site. Runs cron jobs on a daily basis. Appears to be free. Limited to daily jobs.
Webcron.org - Cron job - Crontab online - Monitoring - Accueil
English/German/French access. Offers monitoring and cron job execution. Uses a paid model of $x per job execution.
Online Cron Jobs
French/Romanian/English site. Offers free cron jobs by donation only.
SetCronJob
English site. Offers free and paid cron job execution. Uses a paid model of $x per job execution.

by

CronJobGuy

Hello world. This is my bio. I can edit it later!

Feeling creative? Create a Lens!