At a base level, cron is just a scheduler: you give it a time or a series of times, and it will run something at those times.  For example, "Open my web browser to this website at 3pm every Tuesday" or "Delete my tmp directory every Friday at 6pm".

It's fairly easy to get up and running here, so let's go through some things.  If you don't have Linux or MacOS and are running Windows, you can still do some things that are pretty much the same as cron.

There are also more advanced tricks with cron, this will cover the basics.  I'd recommend at least making one or two cronjobs and see how you like it for a week or two.  I have some recommendations at the end.


Crontab

cron comes from the Greek word χρόνος ("Chronos", meaning "Time") but, frustratingly, does not have the "h" in it for, what I assume, are historical reasons to make program names as small as possible.  Either way, sorry linguists.  

crontab stands for "Cron Table" which, in turn, stands for "Chronos Table", which is just a time-table.  Cute.  The idea here is that all of our cronjobs (scheduled tasks) will appear in the crontab, the time-table.  Let's open it up by using the -e flag, for edit.

$ crontab -e 

You will get a screen telling you to pick an editor.  Whichever is easiest for you.  The crontab will come up and it's a whole ton of information.  Feel free to read this or not; we'll be going over the basics.

Nothing is in here right now, so I took a template from somewhere a while ago and this is what I put in my blank crontab when I start up a new OS:

#crontab -e
SHELL=/bin/bash

# +---------------- minute (00-59)
# |  +------------- 24-hour (00-23)
# |  |  +---------- day of month (1-31)
# |  |  |  +------- month (1-12) 
# |  |  |  |  +---- day of week (0-6) (Sunday=0)
# |  |  |  |  |
# *  *  *  *  *  command

Looks pretty wild, but it's mostly the last line that we care about.  This is how all cronjobs look.  You specify either a number to do something at that particular time\month\day-of-week, or you use * to denote "all".

Simple Cronjobs

Let's check out a few examples.  I'll show the examples here, and then give some quiz questions with answers below.

# Sample 1: runs my_cool_process every day at 1:00 AM.
00 01 * * *  my_cool_process

# Sample 2: runs my_cool_process every monday of the month for every month at 3:40PM.
40 15 * * 1 my_cool_process

# Cronjob Quiz 1
00 01 10 * * my_cool_process

# Cronjob Quiz 2
30 14 * * 3 my_cool_process
Problem 1 Runs my_cool_process at 1:00AM on the 10th of every month.
Problem 2 Runs my_cool_process at 2:30PM every Wednesday.

Cronjobs with "Every X seconds/minutes/days..."

Cool.  One more thing here, what if we have something like, "I wanna run this job every ten minutes."  What do we do then?  You might think we could do 10 * * * * process_name but that will only run at 1:10AM, 2:10AM, ... so it doesn't quite work.  Luckily, cron gives us a nice way to do this:

Adding the / to any wildcard * character gives us a "do every X minutes\hours\whatever" command.  Let's look at a few different cronjobs to see the difference.

# Run process_name at the 20's: 1:20AM, 2:20AM, 3:20AM, ...
30 * * * * process_name

# Run process_name every 20 minutes: 1:20AM, 1:40AM, 2:00AM, ...
*/30 * * * * process_name

# Run process_name at 1:00AM on the 1st of every month.
00 01 1 * * process_name

# Run process_name at 1:00AM every day of every month.
00 01 */1 * * process_name

That little */ is putting in a lot of work.


Ranges

You can also use ranges (00 01-09 * * * process_name, which means run every day at 1:00AM, 2:00AM, 3:00AM, ..., 9:00AM) which might be useful if you want to do things only during working hours.


Examples of Practical Cronjobs

I took some time to list all of the stuff that I was constantly doing manually and tried to automate as much as possible.  Most of these things were productivity-based and saved just a little time, but it was brain-power I didn't need to spend thinking about them.

# crontab -e

# Removes my personal tmp folder every Sunday at 1:00AM.
00 01 * * 0 rm -rf ~/tmp 

# Backs up my blog every Sunday at 2:00AM.
00 02 * * 0 ~/path/to/file/backup_script.sh

# Removes all my "none" tagged docker images daily at 3:00AM.
00 03 * * * docker rmi -f $(docker images | grep "^<none>" | awk "{print $3}") 

Also, help you out, there is a nice website that helps you with cronjob parsing and creation, but you'll quickly be able to do these yourself once you get a little practice


Do you use cron in your day-to-day?  Anything interesting?  Let me know!