Linux Cron Jobs: Schedule Tasks with crontab

Cron is a built-in Linux scheduler that automates repetitive tasks by running commands or scripts at specified times.

System administrators and developers use cron jobs for backups, log cleanup, software updates, monitoring, and scheduled automation.

This guide explains cron jobs, crontab syntax, scheduling patterns, and practical examples.

Concept Overview

The cron daemon runs continuously in the background and checks scheduled jobs every minute.

Users define scheduled tasks inside a crontab (cron table), where each line represents one scheduled job.

Key Concepts

1. Cron Daemon (crond) – Background service that executes scheduled jobs.

2. Crontab – Configuration file containing scheduled tasks.

3. Cron Expression – Defines when a command should execute.

4. Scheduled Job – A command or script executed automatically.

Managing Crontab

The crontab command allows users to create, edit, list, and remove scheduled jobs.

BASH
crontab -e
crontab -l
crontab -r
sudo crontab -e

Explanation

crontab -e opens the current user's cron table for editing.

crontab -l lists all scheduled cron jobs.

crontab -r removes all cron jobs for the current user.

sudo crontab -e edits the root user's cron table.

Understanding Crontab Syntax

Each cron job follows a five-field schedule followed by the command to execute.

TEXT
* * * * * command_to_execute
│ │ │ │ │
│ │ │ │ └── Day of Week (0-7)
│ │ │ └──── Month (1-12)
│ │ └────── Day of Month (1-31)
│ └──────── Hour (0-23)
└────────── Minute (0-59)

Special Characters

* – Every value.

, – Multiple values.

- – Range of values.

/ – Step values.

Common Cron Job Examples

BASH
# Every minute
* * * * * /home/user/script.sh

# Every day at 2:00 AM
0 2 * * * /home/user/backup.sh

# Every Sunday at midnight
0 0 * * 0 /home/user/cleanup.sh

# Every 15 minutes
*/15 * * * * /home/user/monitor.sh

# Every Monday at 9:30 AM
30 9 * * 1 /home/user/report.sh

These examples demonstrate common scheduling patterns used in Linux automation.

Scheduling Shell Scripts

Cron jobs commonly execute Bash shell scripts for automation.

BASH
#!/bin/bash
DATE=$(date)
echo "Backup completed on $DATE" >> /home/user/backup.log

Save the script, make it executable, and schedule it using crontab.

BASH
chmod +x backup.sh
0 1 * * * /home/user/backup.sh

Viewing Cron Logs

Cron activity can be monitored using system log files.

BASH
grep CRON /var/log/syslog
journalctl -u cron
systemctl status cron

These commands help troubleshoot failed or missing scheduled jobs.

Environment Variables

Cron jobs execute with a limited environment, so full command paths should be specified whenever possible.

BASH
PATH=/usr/local/bin:/usr/bin:/bin
MAILTO=user@example.com

Example Workflow

Create a Bash script that performs a backup.

Grant execute permission using chmod +x.

Open the cron editor using crontab -e.

Add a cron schedule to execute the script every night.

Verify execution by checking log files or generated output.

Applications

Automating system backups.

Cleaning temporary files and log rotation.

Running scheduled reports and database maintenance.

Monitoring servers and sending alerts.

Scheduling software updates and maintenance tasks.

Advantages

Automates repetitive administrative tasks.

Reduces manual effort and human errors.

Runs tasks even when users are not logged in.

Supports flexible scheduling options.

Limitations

Cron jobs execute with a limited environment, which may require full command paths.

Incorrect scheduling expressions can prevent jobs from running as expected.

Long-running tasks may overlap if execution time exceeds the schedule interval.

Best Practices

Always use absolute file paths in cron jobs.

Redirect output to log files for easier troubleshooting.

Test scripts manually before scheduling them.

Avoid scheduling multiple resource-intensive jobs at the same time.

Document important cron jobs for easier maintenance.

Mastering cron jobs enables efficient Linux automation and is an essential skill for system administrators, DevOps engineers, and developers.