cron Command in Linux

Introduction to Cron Job Scheduling

This guide explores the cron service in Linux, a powerful time-based task scheduler. Learn to manage scheduled tasks by viewing and editing the crontab, scheduling basic cron jobs, and configuring notifications and logging. Cron is a core Linux feature, with commands that are standard across distributions. By completing this guide, you'll gain practical skills in scheduling and automating tasks using the cron service effectively as a systemadmin.

Understanding the Linux cron Service and Crontab Files

This section dives into the cron service within Linux, a systemadmin's essential tool for scheduling jobs. We'll cover how to view and modify the crontab, the primary configuration file that defines cron schedules. It's important to understand how to use cron effectively for task automation.

First, let's verify the cron service's current status:

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 displayed output confirms that the cron service is up and running properly.

Next, let's examine the crontab associated with the labex user:

crontab -l

This command lists all cron jobs scheduled for the user executing the command. In a new environment, the crontab is typically empty.

To make changes to the crontab, use this command:

crontab -e

This opens the crontab in a text editor, allowing for adding, modifying, or removing job entries. The crontab uses a specific syntax to determine job execution times:

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

The five asterisks hold the following meanings:

  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 represents Sunday)

Adjust these values to define your precise cron job schedule. Use cron to schedule different scripts on your Linux machines.

How to Schedule a Basic Cron Job

This section demonstrates creating a simple cron job that runs a script every minute as an example.

First, let's create the sample script that cron will execute:

nano ~/project/cron_job.sh

Paste the following script content into the file:

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

Save the file and close the editor.

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

crontab -e

Insert this line into the crontab file:

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

This line configures the cron_job.sh script to execute every minute.

Save the changes and exit the crontab editor.

To confirm that the cron job executes correctly, wait one minute and then check the contents of the cron_output.log file:

cat ~/project/cron_output.log

The output should display the current date and time from when the cron job ran.

Cron Job Notifications and Logging Configuration

This section covers how to configure cron to send email notifications with cron job outputs and enable detailed logging for debugging and auditing.

Begin by installing the package required for email notifications:

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

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

crontab -e

Add the following line at the beginning of the file:

[email protected]

This will configure cron to send the output from scheduled jobs to the specified email address.

Now, enable detailed logging for the cron service. Edit the main cron configuration file:

sudo nano /etc/crontab

Look for the following line in the file:

#EXTRA_OPTS=""

Uncomment the line and modify it to:

EXTRA_OPTS="-l 7"

This sets the log level to 7, the most verbose option. Save the changes and exit the editor.

Restart the cron service to apply the configuration changes:

sudo systemctl restart cron

Now all cron job output and error messages will be logged to the /var/log/cron.log file. View the log using the following command:

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

Summary

This guide provided an introduction to the cron service in Linux, a crucial tool for systemadmin tasks. You learned how to view and edit the crontab, the central configuration file for cron jobs. You scheduled a simple cron job to execute a script every minute, and learned to configure cron notifications and logging. You now understand the crontab file format and its fields that define job execution times. With the skills learned, you can effectively manage scheduled tasks on Linux systems, including running scripts as root if required.

400+ Linux Commands