DevOpsLesson
DevOpsLesson

Free, comprehensive DevOps tutorials and learning roadmaps. Master Docker, Kubernetes, CI/CD, and more.

Stay Updated

Get notified about new tutorials and features.

Tutorials

  • What is DevOps?
  • Docker Tutorial
  • Terraform Tutorial
  • CI/CD Pipeline
  • All Tutorials

Roadmaps

  • DevOps Engineer
  • Cloud Engineer
  • SRE Path
  • All Roadmaps

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service

© 2026 DevOpsLesson. All rights reserved.

DOCKERKUBERNETESTERRAFORMAWSCI/CDLINUXGITDEVOPS ROADMAPCLOUD ROADMAPSRE ROADMAPGIT CHEATSHEETDOCKER CHEATSHEETK8S CHEATSHEETTF CHEATSHEETLINUX CHEATSHEETDOCKERFILE LINTERYAML VALIDATORCRON PARSERREGEX TESTER

Linux Tutorial

Introduction to Linux
Linux Filesystem
Linux Navigation
Linux File Operations
Linux Text Processing
Linux Permissions
Linux Users and Groups
Linux Process Management
Linux systemd Basics
Linux Networking
Linux Disk and Storage
Linux Bash Scripting
Linux Package Management
Linux Environment Variables
Linux Cron Jobs
Linux SSH Keys
Linux Firewall Basics
Linux Log Management
Linux Performance Monitoring and Troubleshooting

Linux Cron Jobs

PreviousPrev
Next

Learn how to schedule recurring tasks with cron, understand crontab syntax, and compare cron with systemd timers.

What Cron Is

cron is the traditional Linux scheduler for recurring tasks. It runs commands or scripts automatically at specific times, which makes it useful for backups, cleanup jobs, report generation, and health checks.

If you have ever needed something to run every night at 2:00 AM or every five minutes, cron is one of the first tools to learn.

Managing Your Crontab

Each user can have a personal crontab file.

Open it for editing:

crontab -e

List current entries:

crontab -l

Remove the current user crontab:

crontab -r

Be careful with crontab -r because it deletes all scheduled entries for that user.

Tip: Always run crontab -l before making major changes so you know exactly what is already scheduled.

Understanding Cron Syntax

A cron schedule has five time fields followed by the command:

* * * * * /path/to/command

The fields are:

  1. minute
  2. hour
  3. day of month
  4. month
  5. day of week

Examples:

0 2 * * * /opt/scripts/backup.sh
*/5 * * * * /opt/scripts/health-check.sh
30 6 * * 1 /opt/scripts/weekly-report.sh

These mean:

  • 0 2 * * * = every day at 02:00
  • */5 * * * * = every five minutes
  • 30 6 * * 1 = every Monday at 06:30

Common Cron Job Examples

Backup script

Run a backup every night:

0 1 * * * /usr/local/bin/backup-db.sh >> /var/log/backup-db.log 2>&1

This appends both standard output and errors to a log file.

Log cleanup or rotation helper

15 3 * * 0 /usr/local/bin/archive-old-logs.sh

Health check

*/10 * * * * curl -fsS http://localhost:8080/health || echo "health check failed"

A health check job can test an internal endpoint and alert if the result is unexpected.

Note: Cron runs with a limited environment. Commands that work in your interactive shell may fail unless you use full paths or set needed variables explicitly.

System Cron Directories

Not all scheduled jobs live in a user crontab. Linux systems also include system-wide scheduling locations.

/etc/cron.d/

This directory contains cron files managed by packages or administrators. Files here usually include an extra field for the user account that should run the job.

Example entry:

0 4 * * * root /usr/local/bin/system-backup.sh

/etc/cron.daily/, /etc/cron.weekly/, and friends

These directories hold scripts that run on a daily, weekly, or monthly schedule depending on the system configuration.

You may see:

  • /etc/cron.hourly/
  • /etc/cron.daily/
  • /etc/cron.weekly/
  • /etc/cron.monthly/

Dropping a script into the right directory can be simpler than writing a full cron expression for routine maintenance tasks.

Good Practices for Cron Jobs

When writing cron jobs:

  • use absolute paths such as /usr/bin/python3
  • redirect output to logs or monitoring
  • keep scripts idempotent when possible
  • test the script manually before scheduling it
  • make sure the script has execute permission

A script that depends on your interactive PATH, aliases, or current directory is likely to break under cron.

systemd Timers as a Modern Alternative

Many modern Linux systems use systemd timers as a more structured alternative to cron. Timers pair with systemd service units and often provide clearer logging, dependency handling, and status inspection.

A timer based workflow might look like this:

systemctl list-timers
sudo systemctl enable --now backup.timer
journalctl -u backup.service

Cron is still widely used and worth learning, but systemd timers are often preferred for services that already live in the systemd ecosystem.

A Practical Troubleshooting Flow

If a cron job did not run, check:

  • whether the schedule is correct
  • whether the script path is absolute
  • whether the command works manually
  • whether output was redirected somewhere useful
  • whether the cron service is running on that host

A job that silently fails is often missing environment variables, permissions, or full command paths.

What You Should Remember

Cron is a lightweight and powerful scheduler built around time expressions and shell commands. Use crontab -e to edit, crontab -l to inspect, and system cron directories for broader maintenance tasks.

For modern service-based workflows, systemd timers can offer better observability, but cron remains a core Linux skill.

Test Your Understanding

Exercise

Exercise 1: Reading a cron expression

What does the schedule `*/5 * * * *` mean?

Exercise

Exercise 2: Editing scheduled tasks

Which command opens the current user's crontab for editing?

Exercise

Exercise 3: Limited cron environment

Why do many cron jobs use absolute command paths like `/usr/bin/python3`?

PreviousPrev
Next

Continue Learning

Linux Bash Scripting

Learn how to write practical Bash scripts with variables, conditionals, loops, functions, arrays, redirection, traps, and exit codes.

30 min·Hard

Linux Package Management

Learn how to install, update, remove, and manage software packages with APT, DNF, Snap, and AppImage on Linux.

18 min·Easy

Linux Environment Variables

Learn how environment variables work in Linux, how to export and persist them, and how PATH and .env files fit into daily workflows.

15 min·Medium

Explore Related Topics

Do

Docker Tutorials

Run and manage containers on Linux

Gi

Git Tutorials

Master Git version control on the command line

Try the Tool

Cron Parser

Parse and validate cron expressions with a visual schedule preview.

Regex Tester

Test and debug regular expressions used in Bash scripts and configs.

On This Page

What Cron IsManaging Your CrontabUnderstanding Cron SyntaxCommon Cron Job ExamplesBackup scriptLog cleanup or rotation helperHealth checkSystem Cron Directories`/etc/cron.d/``/etc/cron.daily/`, `/etc/cron.weekly/`, and friendsGood Practices for Cron Jobssystemd Timers as a Modern AlternativeA Practical Troubleshooting FlowWhat You Should RememberTest Your Understanding