Manually running commands or shell scripts to set up and maintain software environments just isn’t going to cut it. At least, not anymore.
Not only is it time-consuming and risky, but it also exposes the company to all sorts of threats.
What’s more, there is no reason you should run regular system maintenance or administration manually when you can automate and schedule them periodically at fixed intervals, times, or dates.
That’s why technical leaders love Cron.
Cron Jobs help you run tasks on a schedule, eliminating tedious, repetitive processes that will take up a considerable chunk of your time and energy.
In this guide, we’ll cover the nuts and bolts of Cron, how it works, and some best practices when configuring your Cron Jobs.
What is the definition of Cron?
Cron refers to Cron Jobs, a nifty little feature of the Unix operating system that lets you schedule any command to be run at any time.
Cron Jobs can schedule work by the minute, the hour, the day of the month (1-31), the month (1-12) and the day of the week (Mon-Sat).
Let's say you have a script you want to run at 5 a.m. every Monday. You could create a second script that runs all the time and waits until five am on Monday before executing your script.
However, this isn’t very convenient, especially when you start adding dozens or even hundreds of scheduled operations. That's just asking for trouble, not to mention the performance hit.
Cron solves this by being the single definitive task scheduler on Unix. Cron Jobs lets you schedule through the crontab (We'll get to what that is in the next section), an extensive list of tasks, each with its own schedule.
It takes care of the nitty-gritty of executing those tasks. What's more, it will send you an email if any of them run into errors.
How does cron work
To understand Cron Jobs, we must first understand the crontab. The operating system uses the crontab to schedule all sorts of tasks.
It lives in the deep low-level kernel and takes up very little resources. Exactly how the crontab works is beyond the scope of this article, but it took a lot of great minds to make it as fast as it is today.
Cron Jobs are based on top of the crontab. The crontab lets you schedule as many tasks as you'd like, and all these tasks are executed by Cron job expressions when the time comes.
How do I create a Cron Job?
Creating Cron Jobs is easy. To start, type the following command into your Unix computer's terminal or terminal emulator:
Next, it will ask you to choose a text editor. The output may look something like this:
Select an editor. To change later, run 'select-editor'.
1. /bin/nano <---- easiest
We recommend using Nano (Usually the first on the list). To select an editor, type its number on the list into the command line and press enter.
Next, you will see a blank text file with some comments (In Cron Job expressions, comments start with a #).
Now enter the Cron expression. A cron expression starts with five characters (The schedule) and is followed by the command you'd like to run.
For example, here is a Cron Job that runs at exactly five am each Monday:
0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
Each Cron Job expression has exactly five characters for the schedule followed by the command.
The schedule looks like this:
0 5 * * 1.
Let's break that down:
- Minute: 0. Run at exactly the zeroth minute and not any other minute.
- Hour: 5. Run at exactly the fifth hour.
- Day: *. All days.
- Month: *: All months.
- Weekday: 1: Monday.
That's how you write an expression for a Cron Job. If you do 1 5 * * 1, then it'll run at 5:01 AM instead. * * * * 1 would run every minute of every hour of Monday.
Cron configuration tips
Below are a few best practices when setting up your Cron Jobs.
1. Use the / character to run between intervals
In the last example, we saw a way to run cron jobs at exactly 5 A.M. on Monday.
However, what if we want to run a Cron configuration every five hours?
In that case, Cron provides us a nifty little tool: /A cron job expression 0 */5 * * 1 would, for example, run every five hours, but only on Mondays.
2. Specify more than one time to run a job
What if you want to run a Cron Job at fixed multiple times in the day? Let’s say, 5 A.M., 6 A.M., and 7 A.M.?
The solution: Use a comma.
0 5,6,7 * * 1 would run at five, six, and seven am on Mondays! Similarly, 0 5 * * 1,2,3 would run on Monday, Tuesday, and Wednesday.
3. Validate your Cron jobs online
So you've written your cron expression, but how do you know it's correct?
After all, if you made a mistake, you won't get an error and it may be hours or weeks before your Cron Job actually runs.
Here's what you can do: Go to a website called Cron Guru (https://crontab.guru/) and type the Cron configuration in there. You’ll get the same thing but in plain English.
Now you can verify whether your expression is correct or not, allowing you to fix it and run it correctly.
What are the drawbacks to Cron Jobs?
Cron Job software is simple yet powerful. You can schedule hundreds of Cron Jobs and let the Cron Job software handle the heavy lifting, saving you tons of time and energy from doing everything manually.
However, if you’re new to Cron, using the Cron Job software can get complicated.
While using Cron Jobs is better than writing your own scheduling scripts, you’re likely to make mistakes, encounter errors that need to be fixed, handle exceptions, and more.
Additionally, if you want to run a command each time there's a new file inside a folder, you can’t use Cron to do it.
The good news, a more advanced and easy-to-use workload automation solution can.
OpCon simplifies the process of automating your commands. You can have more triggers such as new files in a folder and monitor those operations live through a Dashboard. Oh, and remember the part about failed jobs sending you an email? With event-driven automation, you can program a workflow to account for this and automatically rerun the job before alerting an operator.
OpCon combines intense scalability with reliability, allowing you to handle and schedule 150 to 150,000 jobs daily.
Some of OpCon’s features include scheduling or event-driven flexibility functionalities and seamless web service and API integrations with third-party apps.
It also offers comprehensive event logs, allowing you access to contextual data to simplify and streamline your troubleshooting.
All these features and more make creating Cron Jobs less complicated and more efficient.
Dans cet article
Découvrez comment fonctionnent les tâches cron, à quoi elles servent et pourquoi vous devriez envisager de mettre en œuvre une plateforme d'automatisation des processus IT.