Time Is Money: Automate Everything With CRON!

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?

Keeping Tabs

crontab -l

Jobs — Jobs For Everyone!

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 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!

Interested in Computer Science, education and world domination.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store