Cron: What is it and Why Is it Important?
If there is one thing we can all agree on, it's that computer programming technology is constantly changing. One moment you're learning Java Spring, then Spring Boot, and finally AngularJS. And once AngularJS is deprecated, you're required to ramp up on Angular.
However there is one part of programming that has stayed remarkably consistent: job scheduling. Since its inception in 1977, cron has been the go-to solution for scheduling repetitive tasks on *nix operating systems.
If you are vying for a Linux+ certification, or any career related to Linux, it is critical to have a firm understanding of what Cron is and how it works.
For the uninitiated, you may be wondering what kind of "job" would even be scheduled with Cron. Don't worry, we'll go over that. Once we have a concrete example, we'll discuss the cron syntax, and lastly we'll go through our very own example. Without further ado, let's define cron and go over an example.
What is Cron?
Cron is a time-based scheduler used on Linux operating systems. While that definition may seem fairly straightforward, there is a bit more to it than that. After all, time can be measured in dozens of ways. Should the job be triggered every day, every other twenty minutes, or at 10:00 p.m. on the last Friday of the month? Yes, that latter schedule can be configured easily on Cron. To make things more concrete, let's take a look at an example of a job that would need Cron.
What is a Cron Job?
Say you are a software developer working at some giant insurance company. At this company there is a retention policy on customer inquiries. Every time a customer fills out an online auto inquiry form, it's retained in the database for one year. Then it is deleted according to the previously mentioned retention policy.
Since a lot of jobs run on the last Friday of every month, you and your team decided this would be the perfect time to purge the database of old records. In other words, once a month, you delete all records older than one year.
Unfortunately this job needs to run at midnight. Naturally, no one wants to stay up until midnight on the last Friday of every month. This is where Cron comes into play. With Cron, you can trigger the job to run at that exact time every time. No human intervention necessary! Let's take a look at how that would be done.
How Do You Execute a Cron Job?
Cron jobs are easy to execute, but it takes attention to detail to make sure they turn out correct. A misconfigured cron job can become an unmitigated disaster if left unattended.
For instance, a memory intensive job that is supposed to run every month could be misconfigured to run every minute. This could crash servers and is notoriously difficult to troubleshoot. So make sure the job is thoroughly tested before applying it to a production environment.
The picture below is an example of how to read a Cron command:
As shown above, there are five intervals for a cron job. The shortest time is the left-most asterisk (A.K.A wildcard), which is the minute interval. The right most wildcard is the day of the week. One way to interpret the wildcard is to mean the word every. So in the example above, this cron job is running every minute of every hour on every day of every month, seven days a week. Additionally, for the day-of-the-week interval, zero is Sunday and six is Saturday. (7 can be written to mean Sunday as well.)
Now let's say we were to slightly adjust the aforementioned cron job. We rewrite it as the following:
* * * * 6
Remember that 6 is Saturday. Therefore this schedule will run every minute of every hour, every month — but only on Saturday. Now let's say we were to take that exact same schedule and change it to this:
43 * * * 6
This schedule will run on the forty-third minute every Saturday. Just for the fun of it, let's build off that example and study one more:
43 0 4 11 6
Can you guess what the final schedule is? If you guessed that this schedule takes place at 12:43 a.m. every Saturday in November and on the fourth day of November, then you'd be correct!
The more you practice cron jobs, the easier they will get. Remember, it's very important to get these things right. Always verify on a dedicated cron website such as this one.
How Do I Setup A Cron Job?
So far we've discussed what Cron is, the type of job you'd schedule, and how the syntax works. Now is when the rubber meets the road, and we'll run a very basic cron job. We will write a schedule to create a file called "Test4" every minute if it does not already exist. Hopefully you have either a MacOS or a Linux box to follow along.
*Nix systems have a nifty cron job organizer called the crontab. (Tab is short for table.) Crontab is a central location to schedule all of your cron jobs. We will program our schedule into crontab and then watch the magic happen.
First, open up Terminal. Navigate to your Desktop like I do in the picture below:
Notice I just used the cd command to do that. Type pwd and hit enter. Pwd displays the current working directory. Copy it the path. We will be using it shortly.
Next please type crontab -l. This command will display a list of cron jobs that are scheduled on your machine. There is a good chance that there aren't any scheduled, in which case nothing will appear.
Next, type crontab -e. This will open up the cron table. FYI, this is probably going to open a vim editor. These can be fairly esoteric to the unversed. First type i. This will put Vim into Insert Mode, allowing you to type.
Now that the table is open and ready to be edited, type the following command:
* * * * * touch your/Desktop/path/<some-file-to-create>
Once that is typed out, hold shift and hit enter on the keyboard. then type wq and hit enter. (Wq stand for write quit.) That's how to save a file on Vim.
To circle back to cron, we are telling the computer to create (or touch) a file with some name every minute forever. (Don't worry, it will only create it if it doesn't exist, and this job can easily be deleted.) Below is an example of one I created:
We're just about done. Now that we've exited crontab, go ahead and type the crontab -l command one more time. It will display the brand new cron job we created. Wait one minute, and sure enough the file you created will appear on your desktop. Simply type ls to verify.
Teardown the Job
To complete the example, head back into crontab. Delete that line and save it. To delete it, either type d twice while in Edit Mode, or go to Insert Mode and hit backspace until it is deleted. Lastly, delete the file the scheduler had been creating. All done!
We really covered a lot of ground. However, understanding, implementing, and most importantly troubleshooting cron jobs is vital for any career in Linux. Try not to get hemmed up on all the technicalities such as Vim and different Linux commands. Remember, the main goal of Cron is simply to schedule a job and nothing more.
By now it should be readily apparent how incredibly powerful this tool is, and why it has been tried and trusted by every major corporation for many decades — and many more years to come.