Scheduling Repeated Jobs with Cron
Start course
1h 20m

As the title suggests, this course looks at intermediate-level skills for those who already know a bit about Linux but want to enhance that knowledge. In this course, introduce new concepts such as job control, switching users, and installing software. 

Learning Objectives

  • Implement processes and job control, and switch between users
  • Install software using RPM- and Debian-based systems

Intended Audience

  • Anyone with basic knowledge of Linux who wants to learn more
  • Professionals who want to learn more about Linux to enhance their career prospects


This is an intermediate-level course so some knowledge of Linux is expected. If you're just starting out, then try our Linux Fundamentals course first.


This lesson covers the cron service, the crontab, or cron table format, as well as the crontab command. If you need to repeat a task on a schedule, you can use the cron service. The cron service is a time-based job scheduling system that starts when the system boots. Every minute, the cron service checks to see if there are any scheduled jobs to run, and if so, it runs them.

Cron jobs are often used to automate a process or perform routine maintenance. You can schedule cron jobs by using the crontab command. A crontab, or cron table, is a configuration file that specifies when commands are to be executed by cron. Each line in a crontab represents a job and contains two pieces of information. One, when to run, and two what to run.

The time specification consists of five fields. They are minutes, hour, day of the month, month, and day of the week. After the time specification, you provide the command to be executed. This example, crontab entry runs every Monday at 7:00 AM. The command will only be executed when all of the time specification fields match the current date and time.

You can specify that a command be run only once but this is not the typical use case for cron. Typically, one or more of the time specification fields will contain an asterisk or a star, which matches any time or date for that field. This job will run only when the minute is zero, the hour is seven, and the day of the week is one. In the day of the week field, zero represents Sunday, one is Monday, et cetera. This job will run on any day of the month, and during any month, since the asterisk was used for those two fields.

If any output is generated by the command in a cron job, it is mailed to you. You can check your local mail with the mail command. If you would prefer not to get email, you can direct the output of the command as in this example. You can provide multiple values for each of the fields. If you would like to run a command every 15 minutes, you could use 0,15,30,45 in the minutes field, or you could use */15. You can even use ranges with a dash.

If you wanna run a job every minute for the first five minutes of the hour, you can use 0-4 in the minutes field. That job will get executed at the top of the hour, zero minutes, then one minute, two minute, three minute, and at the fourth minute, running a total of five times every hour. There are several implementations of the cron scheduler, and some of those implementations allow you to use shortcuts and keywords in your crontabs.

I'm gonna cover some common shortcuts but these may or may not work on your Linux distribution. The best thing to do is to man cron, and check to see if these will work for you. The @yearly shortcut means to run once a year at midnight on the morning of January 1st. Annually is just the exact same thing as yearly. Monthly means run once a month at midnight on the morning of the first day of the month. Weekly, run once a week at midnight on the morning of Sunday. Daily, run every day at midnight.

Midnight is the same thing as daily, and hourly means to run once an hour at the beginning of the hour or at the top of the hour. The Crontab command manipulates cron jobs. You can use crontab file to install the new crontab from the contents of that file. To list your jobs, run crontab -l, to edit them use crontab -e. Note that crontab -e will invoke the editor that's specified in the editor environment variable. Crontab -r removes all of your cron jobs.

If we run a crontab -l command, it should list our cron jobs. And in this case I do not have any cron jobs scheduled, so let's create one. I'm just going to edit the file with my favorite editor vi. Any line that begins with an octothorp, or a pound sign, or hash, or whatever you wanna call that, is a comment. This will be ignored in shell scripts, it will be ignored by cron, et cetera.

Let's create a time specification, so zero minutes, seven for the hour, and one represents Monday, and we will put the command we want to run, opt/sales/bin/weekly-report. I'll save that to that file. Now we can install that file by running crontab and the file name. Now, when we list our cron jobs, we'll see that we have one cron job.

Let's see, let's look at the editor environment variable. It's not set. Let's set editor to vi. Edit the crontab by running crontab -e, and if I wanna disable this cron job, for instance, I could just comment it out. Another way I can do that is run crontab -r to delete my cron jobs. And you can see that that removed them. The cron service runs scheduled jobs. You can manipulate those jobs with a crontab command.

About the Author
Learning Paths

Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.