Linux cron Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the cron service in Linux, which is a time-based job scheduler. We will learn how to view and edit the crontab, schedule a simple cron job, and configure cron notifications and logging. The cron service is a built-in feature of Linux, and the commands used in this lab are widely supported across different Linux distributions. By the end of this lab, you will have a solid understanding of how to effectively manage and schedule tasks using the cron service.

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/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux/BasicFileOperationsGroup -.-> linux/tail("`File End Display`") linux/SystemInformationandMonitoringGroup -.-> linux/crontab("`Job Scheduling`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") linux/SystemInformationandMonitoringGroup -.-> linux/date("`Date/Time Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/service("`Service Managing`") subgraph Lab Skills linux/tail -.-> lab-422618{{"`Linux cron Command with Practical Examples`"}} linux/crontab -.-> lab-422618{{"`Linux cron Command with Practical Examples`"}} linux/echo -.-> lab-422618{{"`Linux cron Command with Practical Examples`"}} linux/apt -.-> lab-422618{{"`Linux cron Command with Practical Examples`"}} linux/date -.-> lab-422618{{"`Linux cron Command with Practical Examples`"}} linux/service -.-> lab-422618{{"`Linux cron Command with Practical Examples`"}} end

Understanding the cron Service and Crontab

In this step, we will explore the cron service in Linux, which is a time-based job scheduler. We will learn how to view and edit the crontab, which is the configuration file for cron jobs.

First, let's check the status of the cron service:

sudo systemctl status cron

Example output:

● cron.service - Regular background program processing daemon
     Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-04-28 12:34:56 UTC; 1 day 2h ago
   Main PID: 589 (cron)
      Tasks: 1 (limit: 4915)
     Memory: 1.3M
        CPU: 1ms
     CGroup: /system.slice/cron.service
             └─589 /usr/sbin/cron -f

The output shows that the cron service is active and running.

Next, let's view the current crontab for the labex user:

crontab -l

This will list all the scheduled cron jobs for the current user. Since this is a new environment, the crontab should be empty.

To edit the crontab, run:

crontab -e

This will open the crontab editor, where you can add, modify, or remove cron job entries. The crontab file uses a specific format to define when a job should run:

* * * * * /path/to/script.sh

The five asterisks represent the following:

  1. Minute (0-59)
  2. Hour (0-23)
  3. Day of the month (1-31)
  4. Month (1-12)
  5. Day of the week (0-6, where 0 is Sunday)

You can customize these values to schedule your cron jobs as needed.

Scheduling a Simple Cron Job

In this step, we will create a simple cron job that runs a script every minute.

First, let's create a sample script that we will schedule with cron:

nano ~/project/cron_job.sh

Add the following content to the file:

#!/bin/bash
echo "Cron job ran at $(date)" >> ~/project/cron_output.log

Save and exit the file.

Now, let's add the cron job to the labex user's crontab:

crontab -e

Add the following line to the crontab:

* * * * * /home/labex/project/cron_job.sh

This will run the cron_job.sh script every minute.

Save and exit the crontab editor.

To verify that the cron job is running, wait a minute and check the cron_output.log file:

cat ~/project/cron_output.log

You should see the output showing the current date and time when the cron job ran.

Configuring Cron Notifications and Logging

In this final step, we will configure cron to send email notifications for cron job output and enable detailed logging.

First, let's install the necessary package to enable email notifications:

sudo apt-get update
sudo apt-get install -y mailutils

Next, we need to configure the email settings for the labex user. Open the crontab editor:

crontab -e

Add the following line at the top of the file:

[email protected]

This will send the output of any cron jobs to the specified email address.

Now, let's enable detailed logging for the cron service. Edit the cron configuration file:

sudo nano /etc/crontab

Locate the following line:

#EXTRA_OPTS=""

Uncomment it and change it to:

EXTRA_OPTS="-l 7"

This will set the log level to 7, which is the most detailed level. Save and exit the file.

Restart the cron service to apply the changes:

sudo systemctl restart cron

Now, any cron job output or errors will be logged in the /var/log/cron.log file, which you can view using the following command:

sudo tail -n 20 /var/log/cron.log

Summary

In this lab, you learned about the cron service in Linux, which is a time-based job scheduler. You explored how to view and edit the crontab, the configuration file for cron jobs, and how to schedule a simple cron job that runs a script every minute. You also learned about the format of the crontab file and the different fields that define when a job should run. Finally, you configured cron notifications and logging to monitor the execution of your scheduled tasks.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like