Time Is Money: Automate Everything With CRON!

David McIntosh
3 min readJan 20, 2020
Photo by insung yoon on Unsplash

We always hear about innovation as a bi-product of technology and software. That’s one word that always seems to come up in conversation, and rightfully so. Technology tends to promote innovation. Just look at companies like Tesla, who are at the forefront of the electric car and battery industries, thanks to their technological innovations. One equally-important word that doesn’t get as much love, though, is “automation”. Automation, while often met with fear, is an important goal of technology and software development. If we are not also aiming to automate repetitive, rather manual, tasks, then what’s the point? Automation is especially important when managing a software environment. System and application administrators wouldn’t sleep if they had to do everything manually. This is where Cron comes in.

What’s Cron, Anyway?

Cron is a time-based job scheduler in Unix-like systems, used to run commands or scripts at specific intervals (times, dates, etc.). It was invented in the 1970s at AT&T Bell Laboratories, and has since seen various iterations. Some common uses for cron jobs are running of backups, refreshing log files, like that of Apache, or monitoring disk space.

Keeping Tabs

To view the list of cron jobs, a name given to each scheduled task created by Cron, run the following command in the terminal, which will open the crontab, a listing of all schedules tasks currently configured via Cron. Assuming you don’t have any cron jobs currently configured, you should see a message, saying “no crontab for <user>”:

crontab -l

Jobs — Jobs For Everyone!

To create a new cron job, you’ll want to edit the crontab. Run the following command to open the crontab in edit mode:

crontab -e

This will again open the crontab, but in edit mode. To begin editing, and create your first cron job, press “i” on the keyboard, which enables INSERT mode. You will now be able to perform changes to the crontab.

Okay, So What Now?

Cron jobs following a specific format for scheduling. I highly recommend keeping the below image handy if you think you’ll be working with cron jobs a lot in the near future.

Cron job formatting cheat sheet

Now that we’ve seen the format, and we’re editing the crontab, we can begin creating our first cron job. A basic example would be if we had a bash script we need to run every 15 minutes. Maybe this script checks to see if our connection to a specific, vital web service is stable. That seems like something we’d want to know. It would also be impossible to ask an admin to manually execute such a script every 15 minutes! Assuming the script, let’s call it “connection.sh”, exists in ~./scripts, which is where I keep my helper scripts, we can create the following cron job, by typing the following in the terminal window at the very top of the crontab:

*/15 * * * * cd ~/.scripts && ./connection.sh

To save what we’ve done, and essentially create the cron job, press the esc key, then:

:wq

This is saying save/write (w) the changes we’ve made to the crontab and close/quit (q) it.

That’s it! It really is as simple as just those steps we’ve done. You should now have a cron job that executes every 15 minutes, goes to your scripts folder and runs the specified script, “connection.sh”. To create more cron jobs, simply go back into the crontab (crontab -e) and write them on new lines below each other, then save.

The hardest part is knowing how to format the cron job, so that it runs exactly when you want it to. To help with this, I highly recommend visiting crontab.guru, which allows you to enter a schedule and see what the syntax would look like. You can then copy it and apply it to your own cron jobs.

Cron isn’t the only scheduler available, of course, but it’s widely-used and a handy tool to have in your arsenal. I hope this helps you to automate as much as possible all those annoying, but important, tasks that come your way!

--

--

David McIntosh

Interested in Computer Science, education and world domination.