Linux crontab Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the crontab command to schedule recurring tasks in Linux. The crontab command allows you to create, edit, and manage cron jobs, which are scripts or commands that run automatically at specified intervals. You will start by learning about the crontab command and how to check the current cron jobs. Then, you will create a simple script and schedule it to run every minute using crontab. Finally, you will learn how to schedule a recurring backup task using crontab.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/SystemInformationandMonitoringGroup -.-> linux/crontab("`Job Scheduling`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicFileOperationsGroup -.-> linux/rm("`File Removing`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") linux/VersionControlandTextEditorsGroup -.-> linux/vim("`Text Editing`") subgraph Lab Skills linux/cat -.-> lab-422619{{"`Linux crontab Command with Practical Examples`"}} linux/crontab -.-> lab-422619{{"`Linux crontab Command with Practical Examples`"}} linux/echo -.-> lab-422619{{"`Linux crontab Command with Practical Examples`"}} linux/rm -.-> lab-422619{{"`Linux crontab Command with Practical Examples`"}} linux/chmod -.-> lab-422619{{"`Linux crontab Command with Practical Examples`"}} linux/vim -.-> lab-422619{{"`Linux crontab Command with Practical Examples`"}} end

Introduction to Crontab

In this step, you will learn about the crontab command, which is used to schedule recurring tasks in Linux. The crontab command allows you to create, edit, and manage cron jobs, which are scripts or commands that run automatically at specified intervals.

First, let's check the current crontab entries for the labex user:

crontab -l

Example output:

No crontab for labex.

As you can see, there are currently no cron jobs scheduled for the labex user.

Next, let's create a new cron job that runs a simple script every minute. Create a new file called hello.sh in the ~/project directory with the following content:

#!/bin/bash
echo "Hello from cron job!"

Make the script executable:

chmod +x ~/project/hello.sh

Now, let's add a new cron job to run this script every minute:

crontab -e

This will open the crontab editor. Add the following line at the end of the file:

* * * * * /home/labex/project/hello.sh

This cron job will run the hello.sh script every minute.

Save and exit the crontab editor.

To verify that the cron job is running, wait for a minute and check the system log:

tail -n 5 /var/log/syslog

You should see the "Hello from cron job!" message in the log.

Schedule a Recurring Task with Crontab

In this step, you will learn how to schedule a recurring task using the crontab command.

First, let's create a new script called backup.sh in the ~/project directory that will perform a simple backup operation:

#!/bin/bash
echo "Backing up data to backup.txt"
date >> ~/project/backup.txt

Make the script executable:

chmod +x ~/project/backup.sh

Now, let's schedule this script to run every 5 minutes using crontab:

crontab -e

Add the following line to the crontab editor:

*/5 * * * * /home/labex/project/backup.sh

This cron job will run the backup.sh script every 5 minutes.

Save and exit the crontab editor.

To verify that the cron job is running, wait for 5 minutes and check the backup.txt file:

cat ~/project/backup.txt

You should see the current date and time added to the file every 5 minutes.

Manage Crontab Entries

In this step, you will learn how to manage your crontab entries, including listing, editing, and removing cron jobs.

First, let's list all the current crontab entries for the labex user:

crontab -l

You should see the two cron jobs you created in the previous steps:

* * * * * /home/labex/project/hello.sh
*/5 * * * * /home/labex/project/backup.sh

To edit the crontab, use the following command:

crontab -e

This will open the crontab editor, where you can add, modify, or remove cron job entries.

Let's try to remove the hello.sh cron job. In the crontab editor, locate the line * * * * * /home/labex/project/hello.sh and delete it. Save and exit the editor.

To verify that the hello.sh cron job has been removed, run the following command again:

crontab -l

You should only see the backup.sh cron job remaining.

Now, let's disable the backup.sh cron job by adding a # at the beginning of the line:

crontab -e

Add a # at the beginning of the line */5 * * * * /home/labex/project/backup.sh. Save and exit the editor.

To verify that the backup.sh cron job has been disabled, run the following command:

crontab -l

You should see the line for backup.sh with a # at the beginning, indicating that it is now disabled.

Summary

In this lab, you learned about the crontab command, which is used to schedule recurring tasks in Linux. You started by checking the current crontab entries for the labex user and found that there were no cron jobs scheduled. You then created a simple script called hello.sh and added a new cron job to run it every minute. To verify that the cron job was running, you checked the system log and saw the "Hello from cron job!" message. Next, you created a new script called backup.sh that would perform a simple backup operation, and you scheduled it to run every 5 minutes using crontab.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like