Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 51

Scheduling Tasks with cron and at

Recommended resources

Hello! Welcome back to our Linux administration course.

In our last lesson, we learned how to actively manage services with systemctl, controlling processes that are designed to run continuously in the background. Now, we'll shift our focus to automation by learning how to execute commands and scripts at a specific future time or on a repeating schedule.

This lesson directly addresses the learning outcome: Schedule automated tasks using cron and at commands. This is a fundamental skill for any Linux administrator, allowing you to automate maintenance, backups, reporting, and other routine jobs. Mastering these tools is essential for efficiency and is a key topic for your CompTIA Linux+ exam.

We will cover two primary utilities:

  • at: For scheduling a command to run once at a specified future time.
  • cron: For scheduling commands to run repeatedly at regular intervals (e.g., every day, every hour, once a week).

1. One-Time Tasks with at

Imagine you need to run a resource-intensive script, but you want it to execute after business hours to avoid impacting users. Or perhaps you need to restart a server in 10 minutes but want to log off now. This is where the at command is perfect. It schedules a job for a single future execution.

The at command relies on a background service, or daemon, called atd. Just like the services we managed in the previous lesson, atd must be running for your scheduled jobs to execute. You can verify its status with sudo systemctl status atd.

Let's see how to use it in practice.

Cron, Crontab, & At (Linux+ Objective 1.4.2)

The video "Cron, Crontab, & At" by Shawn Powers provides a great demonstration of the at command's workflow.

Watch the video from 01:29 to 05:15. The presenter will show you how to: Schedule a job for a future time (e.g., at now + 2 minutes). Enter commands into the interactive at prompt. Finalize and schedule the job using Ctrl-D. View the queue of pending jobs with atq. Remove a job from the queue with atrm.

Key at Commands

To summarize the video and provide a quick reference:

Command Purpose
at <time> Schedules a job. <time> can be flexible, e.g., now + 5 minutes, 10:30pm, tomorrow.
atq Lists all pending jobs in the queue, showing their job ID and run time.
atrm <job_id> Removes a specific job from the queue using its ID number.
Ctrl-D Exits the interactive at prompt and saves the job.

The batch command, mentioned briefly in the video, is part of the at system. It schedules jobs to run only when the system load is low. While it's good to know it exists, it's far less common in modern practice than at or cron.


2. Recurring Tasks with cron

While at is for one-off tasks, the real power of automation comes from cron, which handles recurring jobs. cron is driven by a daemon, crond, that wakes up every minute to check for scheduled tasks to run.

The cron Architecture

Before diving into the commands, it's helpful to understand the overall structure of cron on a Linux system, which aligns with your preference for a theoretical overview. There isn't just one place to define a cron job.

Cron on Linux Explained with Examples
This diagram shows the different components of the cron system. The `crond` daemon is central. It checks for jobs defined in user-specific files (`var/spool/cron`), a system-wide file (`/etc/crontab`), an include directory (`/etc/cron.d`), and special time-based directories (`/etc/cron.daily`, etc.).

We'll focus on the two most important methods for an administrator:

  1. User crontab files: Each user can have their own list of scheduled jobs.
  2. System-wide cron files: Located in /etc, these are for system-level tasks.

Method 1: User crontab (The Common Way)

As a user or administrator, you will most often manage jobs using your own "cron table" or crontab. You interact with it using the crontab command.

  • crontab -e: Edit your crontab file.
  • crontab -l: List the current jobs in your crontab.
  • crontab -r: Remove your entire crontab (use with caution!).

The most challenging part of using cron is the syntax for specifying the schedule. A cron job definition is a single line with six fields: five for the time and one for the command.

Crontab File Example with Job Definition
This image shows a sample crontab file. Notice the comments at the top explaining the syntax and the cron job definition below, which has five time fields followed by the command to be executed.

Let's break down the syntax with a detailed walkthrough.

Linux/Mac Tutorial: Cron Jobs - How to Schedule Commands with crontab

Corey Schafer's tutorial "Cron Jobs - How to Schedule Commands with crontab" is excellent for mastering the scheduling syntax. We'll watch it in parts to build up our understanding.

First, watch from 01:43 to 07:35. This segment covers the five basic time fields and how to use exact values to define a schedule.

The five fields, in order, are:

  1. Minute (0-59)
  2. Hour (0-23)
  3. Day of Month (1-31)
  4. Month (1-12)
  5. Day of Week (0-7, where both 0 and 7 represent Sunday)

An asterisk (*) in a field means "every". So, * * * * * means every minute of every hour of every day...

Now, let's learn about the special operators that make cron so flexible.

Linux/Mac Tutorial: Cron Jobs - How to Schedule Commands with crontab

Let's continue with the same video to learn about operators for ranges, lists, and intervals.

Watch from 07:35 to 13:26. This part explains: The comma (,) for specifying multiple values (e.g., 1,15 for the 1st and 15th). The hyphen (-) for specifying a range (e.g., 1-5 for Monday to Friday). The forward slash (/) for specifying steps or intervals (e.g., */15 for every 15 minutes).

Test your understanding!

You want to run a backup script located at /usr/local/bin/backup.sh. The script needs to run at 2:30 AM every Monday and Friday.

What would the crontab entry look like?

Show answer

The correct crontab entry is:

30 2 * * 1,5 /usr/local/bin/backup.sh

Breakdown:

  • 30: At 30 minutes past the hour.
  • 2: During the 2 AM hour.
  • *: On any day of the month.
  • *: In any month.
  • 1,5: On Monday (1) AND Friday (5).
  • /usr/local/bin/backup.sh: The command to execute.

A fantastic tool for building and verifying your cron schedules is Crontab Guru. You can type in a schedule and it will translate it into plain English. Both videos recommended it, and it's a great resource to bookmark.

Method 2: System-wide cron

System-wide cron jobs are intended for services and system administration tasks, not individual users. You'll find them in a few places in the /etc/ directory. The key difference is that system cron files require a sixth field specifying the username to run the command as.

Cron, Crontab, & At (Linux+ Objective 1.4.2)

Let's return to the Shawn Powers video. He does a great job explaining the different system-wide cron configurations, which is critical knowledge for the Linux+ exam.

Watch from 17:14 to 28:21. Pay close attention to: The format of /etc/crontab and the extra username field. The purpose of the /etc/cron.d/ directory for packages to add their own cron jobs. The purpose of the /etc/cron.hourly, cron.daily, cron.weekly, and cron.monthly directories, which simply run any executable scripts placed inside them on a schedule.

To recap the system-wide locations:

  • /etc/crontab: The main system crontab file. You should generally avoid editing this directly.
  • /etc/cron.d/: The preferred location to place custom system-wide cron files. Each file here must specify the user to run the job as.
  • /etc/cron.daily/, /etc/cron.weekly/, etc.: Simple directories. Any executable script you place here will be run daily, weekly, etc. You don't use cron syntax in these files.

3. Access Control with .allow and .deny

On a multi-user system, you may want to control who can schedule at and cron jobs. This is managed through two simple files for each service: at.allow/at.deny and cron.allow/cron.deny.

Job Scheduling in RHEL – cron and at Explained with ...

The article "Job Scheduling in RHEL" by freeCodeCamp has concise sections explaining how these access control files work. We'll read the relevant parts for both at and cron.

First, in the section "Job Scheduling using at", find and read the subsection titled "at.allow and at.deny". Then, in the section "Job Scheduling using crontab", find and read the subsection titled "cron.allow and cron.deny". Focus on the precedence rules: if .allow exists, only users listed in it are permitted. If .allow does not exist but .deny does, anyone not listed in .deny is permitted.


Conclusion

You have now learned how to harness two of Linux's most powerful automation tools. This moves you from being a reactive administrator to a proactive one, saving time and ensuring consistency.

Key Takeaways:

  • at is used to schedule a single, one-time task in the future. You manage it with at, atq, and atrm.
  • cron is used for recurring, scheduled tasks. The crond daemon runs every minute to check for jobs.
  • User crontabs are managed with crontab -e and use a five-field time format.
  • System crontabs (like in /etc/cron.d/) add a sixth field for the username.
  • Special directories like /etc/cron.daily offer a simpler way to schedule scripts without using cron syntax.
  • Access to at and cron can be restricted for users via .allow and .deny files in /etc/.

Preview of the Next Lesson:

We've just learned how to schedule commands, but what if you need to run a sequence of commands with logic, variables, and conditions? In our next lesson, we will dive into shell scripting, where you'll learn to write your own basic bash scripts—the perfect tool to be scheduled with cron