Getting started with cron

Dhathri Vupparapalli
8 min readJan 11, 2022

--

Welcome!! In this article, we will be seeing about cron and how to schedule jobs in cron.

Introduction to Cron

The cron service allows us to schedule commands to run at regular intervals like every 5 minutes, every day at 12 pm, every 2nd of the month, every 5th of the month, every January 1st, etc. Usually, you will do things like scheduling backup or some script that goes through all the files every month and cleans up all the unwanted files. To know more about cron, just run the following command in your terminal.

man cron

But, how do we schedule the job for every 30 minutes or every Monday or every December 15th? Well, for that we need to edit a specific file known as a crontab, a configuration file that is an actual file on your machine. Since it is a normal configuration file, we can open it with some text editor or nano, but the recommended way to open this file is using a command called that is crontab -e.

U will get something like this or you may be getting a bunch of comments in this configuration file. Now, this is where all the cronjob commands are written. Let's see a few syntaxes on these cronjobs.

CronJob Syntax

To schedule jobs in crontab configuration file, we have a syntax to follow. This is how the syntax looks like.

CronJob syntax

There are five values that we provide seperated by some number of spaces. We could have one space or 10 spaces in between. The command can be anything that comes at the end will be executed at some interval. We are going to focus on those letters, the first five values, and what they are actually indicating. “a” represents minutes. So, if we have number here, that will be the minute that we are scheduling something for. 0–59 indicates minutes starting from 0 till 59 which makes it 60 minutes in total. Then we have “b” which represents hour. Starting from 0 till 23 which makes 24 hours in total. Then we have “c” which represents Day. Technically, it means the date of a month. Starting from 1 till 31, completing a month. Then we have “d” which represents month, one through 12. You can also use shorthand words like April, May, but u can also use numbers one through twelve. Finally we have “e” which represents the Day of the week. This day of the week is from 0 to 6 in which, 0 is sunday and 6 is saturday. Ok, now this seems simple enough, hopefully, but then we have some special characters and special syntax that we can use at each one of these spots.

Cron characters

We will see about these special characters later in our article, but you will see things like a star, which menas any value. So a “*” at the end for a month in the above picture means any month, a.k.a every month this year. A * for the day of the week means every day of the week.

Let’s start with some simple ones here.

An example to run a job using cron.

In the above picture, we have 30 in the minutes spot and then four continous stars for hour, day, month, dat of the week respectively. Remember, a * means every or all. So, every hour, every day, every month, every day of the week and now a minute is 30. And you might think this means run something for every 30 minutes. That’s not quite what it means. Instead, this means run our command every hour, day, month,week, where the minutes on the clock are exactly 30. So, that could be 1:30, 2:30,3:30,4:30 and so on. EVery single hour of the day of every month, every day of the week where clock shows 30 in the minutes position. So that’s not the same as every 30 minutes. This is actually just running once every hour where the clock is 30 minutes. So it will run at 1:30 but not again until 2:30. So, hopefully, that makes sense.

An example to run a job using cron.

Here’s another example. So, this one says every day of every month where minute is exactly zero and hour is exacty zero. That is mid night 0 0 everyday, every month. So, everyday at midnight run this command, whatever the command is.

An example to run a job using cron.

Now, here is another one. Here we have minute 30, hour 6 and then every day, every month, every day of the week. So, this is going to run, whatever the command is here, every day at exactly 6:30 am. cron uses 24 hour format not 12 hour format. So. 6:30 is only one possible time.

An example to run a job using cron.

Let’s see another example. Similar, right? 6 in the hour, 30 in minute, also we have 1 at the day of the week, which means monday. So from left to right when the clock shows thirty in the minutes position, six in the hour positions are 6: 30 in the morning. Every day. Every month, and day of the week is 1. So, this says every monday at 6:30 in the morning .

An example to run a job using cron.

Here’s one more example. In this example, we have 4 for the month and the month does start at one and does not start at zero. 1st month is january, 2nd is february and 4th month would be April. We still have 1 for the day of the week. So, this means monday in April when the clock shows 6 for hours and 30 for minutes. So every monday in April at 6:30 a.m.

Very first Cronjob in crontab configuration file

The first thing we will do is to just echo some content into a file every minute. To do this task, we need to edit the crontab configuration file using crontab -e command. This is where we start writing our cron jobs. So, the simplest thing that we can do is use the star character, which is goinig to mean every minute of every hour of the day, every day of month, every day of the week and then the command. Here, we gave a very simple echo command which prints a minute along with the date and we are redirecting the output into time.log file in Desktop that doesnot exist yet. So, to save you can use control S or do Control O to write out and then ctrl X to exit.

Cronjob in crontab configuration file

Now, just wait for one minute to pass and then open time.log file in desktop. So, in the below picture we can see the output of the cronjob which says, A minute date is and then friday along with the date and time 23:07:00.

If you wait for another minute and then if you try to open the file again we can see another entry, with the updated time. 23:08:00 and so on. If you want to stop the command from exetion, comment the command and save the file.

Now, let’s run another job that runs at minute 12 only for every hour, of evry month, every day of the week and then will write an echo command and redirect the output into time.log file using “>” as shown below and save thte file.

Now, try to open time.log file and you should be able to see the echo statement which is “It is 12 minutes past the hour”. Which means the above command has run when the clock shows 12 in minutes.

Handling errors in cron job

What if I have some job scheduled, it runs in the middle of the night or runs once a year, who knows when it runs at some point and something goes wrong. But if there’s an error and it could be something very trivial, like I just typed something incorrect when I set up the job.But also we could have these very complicated scripts that are scheduled to run nd it’s not that uncommon for there to be errors and issues. Lets open up crontab -e and let’sdeliberately introduce an error, something we know would be a problem.

Here, in the firts line, instead of echo, we gave some randoom text and lets see how the errors are going to get displayed. Save the file and then try to open the time.log file.

As we can see, we got an error in the time.log file which says “command not found”. This is how we identify the errors in cronjobs. We just fix the spelling and we should be good to go.

Conclusion

In this article, we have seen what is a cron and we have also seen few examples on how to schedule cron jobs. We have tried to run few cronjobs by editing the crontab configuration file using crontab -e command. We can also create simple way of scheduling a backup that happens maybe every day or once a week for a particular folder or folders.

--

--