How to create cron job in Linux

This article is all about how to create a CRON Job in Linux server to automate your processes. It is mostly used for maintenance purposes as it runs in the background.  

Prerequisite

Table of Content

Table of Contents

Create a CRON Job in Linux

What is cron job in Linux?

Cron = is a job-scheduling daemon that is used to schedule tasks to execute in the future.

Cronjob = Its the task that cron schedules.

So, to highlight the difference between cron and cron job is that the former is a service, and the latter is a scheduled task. 

Where is cron tab in Linux?

Crontab = is a file in the Linux server where all the cron jobs are listed. Each user profile would have its own crontab where their scheduled tasks are listed.  

Where is cron tab? The crontab file is located in the /var/spool/cron/crontabs/ directory.  

Install Cron Service in Ubuntu

Let’s install cron in Ubuntu Linux. Execute the following command in the terminal:  

				
					sudo apt update 

sudo apt install cron 

sudo systemctl enable cron 
				
			

How does cron work in Linux?

To create cron job, you need to set 2 parameters.  

  1. The time to execute the task. 
  2. The task to execute.  

The time you set is recurring and could be set with extreme specificity. For scheduling, the Linux cron format takes in 5 details. Minute, hour, day, month, and day of the week.  

Field 

Allowed Values 

minute 

0-59 

hour 

0-23 

Day of the month 

1-31 

month 

1-12 or JAN-DEC 

Day of the week 

0-6 or SUN-SAT 

* 

ALL 

Linux cron format example is shown below:  

				
					# minute    hour    day_of_month    month    day_of_week    command_to_run 
				
			

DO NOT RUN BELOW COMMAND ON YOUR CONSOLE DIRECTLY

Below is an example of a cron job where it is fetching the content of a website every Friday at 1:30 PM.   

				
					30 13 * * 5 curl http://progrmatically.com
				
			

The following is if you want to run a job in crontab every 5 minutes. 

				
					5 * * * * echo 'Hello World' >> /home/Desktop/file1.txt 
				
			

Managing Crontabs

Previously we saw how to create cron job expression for a task. Afterward, you need to add this to the crontab for it to take effect. 

How to Create a Cron Job

As mentioned earlier, a crontab is a list that holds all the cron jobs scheduled to be execute later. To edit the crontab, its recommended to use special commands. So that you don’t have to open the crontab file and add it manually.  

To open and edit crontab, execute the following command: 

				
					crontab -e 
				
			

If you are running the above command for the first time, it will ask you to set the default text editor for that user profile, like so: 

				
					OUTPUT 

no crontab for sammy - using an empty one 

Select an editor.  To change later, run 'select-editor'. 

  1. /bin/nano        <---- easiest 
  2. /usr/bin/vim.basic 
  3. /usr/bin/vim.tiny 
  4. /bin/ed 

Choose 1-4 [1]: 
				
			

Press 1 and hit Enter

The next time you run crontab -e, it won’t ask for it again. Once you are inside the editor, you can edit crontab and add your cron expression. Save it and exit. 

To view or list cron jobs in linux, run the following command: 

				
					crontab -l 
				
			

To erase all cron jobs from the crontab, run the following command: 

Note: the following command does not ask for permission before erasing all cron jobs. Make sure you want to remove all of it. 

				
					crontab -r 
				
			

Schedule Multiple Tasks in a single Cron Job

To allow to execute multiple tasks at the same scheduled time can be done by separating tasks with a semi-colon ( ; )

				
					* * * * * curl http://progrmatically.com; echo 'Hello World' >> file1.txt 
				
			

Special characters in scheduling a cron job

A) Asterisk ( * ) : An asterisk means ALL in cron expression. An asterisk is a wildcard. So, a job scheduled at * * * * * will run every minute of every hour of every day of every month. 

B) Multiple Values ( , ) :  a comma basically allows to add multiple time frames for the same job. So for instance, if you want a job to run at 1st minute and 30th minute of the same hour, you won’t create 2 cron jobs with different timings. Instead, you could use comma like this: (1,30 * * * * …) 

C) Define range ( – )  : Hyphen is use to run the same cron job for a range of different time. So lets say you want to run a job for 1st, 2nd, 3rd, 4th… till 30th min of the hour. You could use comma, sure. But better would be to use hyphen as a range like so: (0-29 * * * * ….) 

D) Step Value ( / ) : a forward slash used with an * introduces a step value. So for example, if you need to run a job every 3 hours, you could do something like this: (0 */3 * * * ….) 

Alternatively, there is an online cron tab generator tool that creates the cron expression for you. Check it out here. 

Online Cron Job Expression Tool
Online Cron Job Expression Tool

TIME STAMP IN CRON

Time Stamps in Cron Jobs are quick way to schedule tasks, but at a fixed time. 

Time       Same as
@yearly 0 0 1 1 * @daily 0 0 * * * @hourly 0 * * * * @reboot Run at startup.

1. Schedule tasks to execute YEARLY

				
					
@yearly echo 'Hello World' >> file1.txt 
				
			

2. Schedule tasks to execute MONTHLY

				
					
@monthly echo 'Hello World' >> file1.txt 
				
			

3. Schedule tasks to execute WEEKLY

				
					
@weekly echo 'Hello World' >> file1.txt 
				
			

4. Schedule tasks to execute DAILY

				
					
@daily echo 'Hello World' >> file1.txt 
				
			

5. Schedule tasks to execute WEEKLY

				
					
@weekly echo 'Hello World' >> file1.txt 
				
			

6. Schedule tasks to execute HOURLY

				
					
@hourly echo 'Hello World' >> file1.txt 
				
			

7. Schedule tasks to execute ON REBOOT

				
					
@reboot echo 'Hello World' >> file1.txt 
				
			

And that’s a wrap! 

These commands will only work on a LINUX terminal. And a common way to run Linux with Windows is to start a Virtual Machine using VMware. 

I hope this article helped you How to Create a CRON Job in Linux Ubuntu. You may also want to read about How to Transfer Files with SSH and How to Access VM Using SSH Keys on AWS. Please like this article and leave your reviews in the comment section below.  

 

Have a great one!