0%

0/1 Lessons

Introduction to Linux Server Operating Systems

• 30min

0 / 3 lessons complete

Linux Server Installation and Lab Setup

• 23min

0 / 6 lessons complete

Working with the Linux Command Line Interface

• 1hr 30min

0 / 12 lessons complete

User and Group Management

• 44min

0 / 7 lessons complete

Linux Storage

• 30min

0 / 6 lessons complete

Linux Administration Basics

• 53min

0 / 8 lessons complete

Linux Networking

• 47min

0 / 8 lessons complete

Course Conclusion

• 5min

0 / 1 lessons complete

Automating Tasks with Cron Jobs

Instructions

Q&A (0)

Notes (0)

Resources (0)

Saving Progress...

Resources

There are no resources for this lesson.

Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.

Create note

In this lesson, learn about Linux cron job and crontab. Cron jobs are a fundamental aspect of Linux that allow you to schedule scripts or commands to run automatically at specified times or intervals.

You will learn about the workings of cron jobs, their syntax, and practical applications. Specifically, you'll create a cron job to execute the backup script we created earlier every Sunday at midnight. Additionally, you will learn how to view cron logs, and log cron job output to a file.

Understanding Cron Jobs

Cron jobs are essential for automating repetitive tasks in a Linux environment. Each user on a Linux system can have their own crontab (cron table) to schedule tasks.

Cron Job Syntax

A cron job is defined by a line in a crontab, following this structure:

* * * * * /path/to/command

The five asterisks represent different units of time: minute, hour, day of the month, month, and day of the week, respectively. By replacing these asterisks with specific numbers or ranges, you can control when the cron job runs.

Creating a Weekly Backup Cron Job

Let's schedule your backup script to run every Sunday at midnight.

  1. Open the root users crontab file by prefixing your command with sudo:
sudo crontab -e
  1. Add the following line:
0 0 * * 0 /root/backup_script.sh

This tells cron to run /root/backup_script.sh at 00:00 (midnight) every Sunday.

  1. Save and exit the editor. Cron will update its schedule based on this.

Verifying Your Cron Job

To see your current cron jobs:

sudo crontab -l

Your backup script should be listed.

Logging Cron Job Output

To log the output of your cron job to a file:

  1. Modify the cron job line in your crontab:
0 0 * * 0 /root/backup_script.sh > /var/log/backup_script.log 2>&1

Here, > /path/to/logfile redirects standard output to a log file, and 2>&1 redirects standard error to standard output, effectively logging all output to your specified file.

For testing purposes, you can set the first block to all asterisks as shown below so it will execute once per minute. BECAREFUL because this could fill up your disk drive if you don't change these settings back

* * * * * /root/backup_script.sh > /var/log/backup_script.log 2>&1

Here's a breakdown of the cron syntax:

  1. Minute: Ranges from 0 to 59.
  2. Hour: Ranges from 0 to 23, where 0 represents midnight.
  3. Day of Month: Ranges from 1 to 31. Note that not all months have 31 days.
  4. Month: Ranges from 1 to 12, where January is 1.
  5. Day of Week: Ranges from 0 to 6, where Sunday is either 0 or 7.

Each time field can have:

  • A single value, e.g., 5 in the hour field means 5 AM.
  • A list of values separated by commas, e.g., 1,3,5 in the day of the week field means Monday, Wednesday, and Friday.
  • A range of values, e.g., 1-5 in the day of the week field means Monday to Friday.
  • A step value, e.g., */10 in the minutes field means every 10 minutes.

Here are some examples:

  • 0 5 * * * /path/to/command: Run the command every day at 5:00 AM.
  • */10 * * * * /path/to/command: Run the command every 10 minutes.
  • 0 0 1 * * /path/to/command: Run the command at midnight on the first day of every month.
  • 0 0 * * 0 /path/to/command: Run the command every Sunday at midnight.

Viewing Cron Logs

To view logs of cron jobs, you can check the syslog:

sudo grep CRON /var/log/syslog

This command filters the syslog for entries related to cron, allowing you to see when and how cron jobs have been executed.

You can also tail the syslog for cron logs:

sudo tail -f /var/log/syslog | grep CRON

You can also tail our new log file:

Server Academy Members Only

Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments