How to Create and Setup a Cron Job in Ubuntu

You will be wondering what is cron job? Cron is a time-based job scheduler in Unix-like operating systems. It allows users to schedule tasks to run periodically at specific times, dates, or intervals. Cron jobs are defined in crontab (cron table) files, which store the schedule and the commands or scripts to be executed.

This article will equip you with the knowledge and skills to create and set up cron jobs effectively, transforming your Linux experience into a realm of efficiency and productivity.

Please note that I will be demoing all commands on Ubuntu 22.04 for this guide

Creating a Cron Job

To create a cron job in Linux Ubuntu, follow these steps:

Step 1: Open the Crontab Editor

Open a terminal window by pressing `Ctrl + Alt + T` or searching for “Terminal” in the Ubuntu Applications menu. Once the terminal is open, type the following command to open the crontab editor:

$crontab -e
crontab -e

This command will open the default text editor (usually Nano) with the crontab file loaded. If it’s the first time you’re running it, you will see the following output. 

crontab -e

Not to worry, just select your preferred editor, I selected nano. 

Crontab Editor

Step 2: Add a New Cron Job

In the crontab file, each line represents a single cron job. The syntax for defining a cron job is as follows:

* * * * * command_to_execute

Here is the breakdown:

The five asterisks represent the schedule for the cron job in the following order: minute (0-59), hour (0-23), day of the month (1-31), month (1-12), day of the week (0-7, where 0 and 7 represent Sunday).

Replace `command_to_execute` with the actual command or script you want to run.

For example, to run a script named `backup.sh` located in the `/home/user/scripts` directory every day at 2:00 AM, add the following line to the crontab file:

0 2 * * * /home/user/scripts/backup.sh

Crontab Editor

Step 3: Save and Exit

After adding the cron job, save the changes and exit the text editor. In Nano, you can do this by pressing `Ctrl + X`, then `Y` to confirm the changes, and `Enter` to return to the terminal.

Example: Create a Cron Job to write a message to a file every minute

To set up a cron job in Linux Ubuntu for writing a message to a file every minute, you can use the crontab command in your terminal to edit the cron table. If you’re using the crontab command, type `crontab -e` to open the cron table in your default text editor. Then, add the following line to the file:

* * * * * echo "This is a message written every minute" >> /path/to/your/file.txt
Crontab -l

Replace `/path/to/your/file.txt` with the actual path to your file like in the screenshot. This cron job configuration will execute the `echo` command every minute, appending the specified message to the designated file. After adding this line, save and exit the file. The cron job will now be set up to run as per your specified schedule.

Crontab -l

Verifying Cron Jobs

To verify that your cron job has been successfully added, you can list the contents of the crontab file by running the following command:

$crontab -l

This command will display the current cron jobs defined in the crontab file.

Crontab -l

With this verification, you will see the defined cron jobs executed as per the definition.

Conclusion

Setting up cron jobs in Ubuntu allows you to automate routine tasks and streamline your workflow. By following the steps outlined in this guide, you can create and manage cron jobs effectively, saving time and effort in the long run. Whether it’s system maintenance, data backups, or application updates, cron jobs offer a convenient solution for scheduling tasks on your Ubuntu system.

Opt for Ultahost’s unmanaged VPS hosting if you desire complete control over your virtual private server. Build a reliable and proficient in-house team to autonomously manage and upkeep your VPS, eliminating the need for reliance on Ultahost’s services.

FAQ

What is a Cron Job?
How do I create a new Cron Job?
How do I specify the time and date in a Cron Job?
How can I view my existing Cron Jobs?

Related Post

How to Install ELK Stack on Ubuntu

The ELK Stack, comprising Elasticsearch, Logstash, and ...

How to Install Kubernetes on Ubuntu 22.04

Kubernetes is an open-source container orchestration sy...

How to Install Composer on Ubuntu 22.04

Composer has become an essential tool in PHP developmen...

How to Install SQLite on Ubuntu 22.04

As a system administrator or developer, you are likely ...

How to Install Vault on Ubuntu

When it comes to securely managing sensitive data such ...

How to Install Wireshark on Ubuntu

Wireshark is a powerful and free network protocol analy...

Leave a Comment