Cron Jobs in Linux: Practical Guide for Web Developers

Web developers frequently use Unix-like operating systems (Ubuntu,
CentOS, etc.) to manage servers. Linux provides many tools for
automation and system maintenance. One of the most important is
cron.

Cron is a time-based job scheduler used to run tasks automatically at
fixed intervals.


What Is Cron

  • Cron is a background daemon that executes scheduled commands.
  • The schedules are defined in crontab files.
  • Cron checks these schedules every minute and runs tasks when their
    conditions match.

Key characteristics:

  • Lightweight when idle.
  • Highly reliable.
  • Automatically restarts if it fails.
  • Executes scheduled commands with system privileges.

Equivalent concept in Windows: Task Scheduler.


Common Use Cases

Cron is used for recurring tasks such as:

  • Task automation
  • Database backups
  • Log rotation
  • Email checks
  • File downloads
  • Report generation
  • Search index updates
  • External API checks
  • Maintenance scripts

If a task must run repeatedly, it is a candidate for a cron job.


Creating Cron Jobs

There are two primary methods.


Method 1: Using the crontab Command

User cron jobs are managed with the crontab command. The actual files
are stored in:

/var/spool/cron/crontabs/

These files should not be edited manually.

Open the Current User's Crontab

crontab -e

This opens the user's crontab file in a text editor.

Typical structure:

# m h dom mon dow command
* * * * * echo "Run every minute"

Where:

Field Description


m Minute
h Hour
dom Day of month
mon Month
dow Day of week
command Command to execute

Example:

0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

Runs every Monday at 05:00.

Save Changes

Once saved, the cron daemon automatically reloads the configuration.

No restart is required.


Method 2: Using Preconfigured Cron Directories

Linux also supports predefined cron directories:

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

Placing an executable script in one of these directories schedules it
automatically.

Example:

/etc/cron.daily/myCronScript

This script runs once per day.

These jobs:

  • Run system-wide
  • Execute as root
  • Are triggered by /etc/crontab

Example entry from /etc/crontab:

17 * * * * root run-parts /etc/cron.hourly
25 6 * * * root run-parts /etc/cron.daily
47 6 * * 7 root run-parts /etc/cron.weekly
52 6 1 * * root run-parts /etc/cron.monthly

Execution order inside these directories follows ASCII filename
order
.


Managing Cron Jobs

List Current User Jobs

crontab -l

View Another User's Jobs

crontab -u username -l

Cron Job Syntax

User crontab syntax:

minute hour day-of-month month day-of-week command

System-wide syntax:

minute hour day-of-month month day-of-week user command

Example task:

Run a search index update once per day:

0 0 * * * python manage.py update_index

Scheduling Options

Each time field supports multiple formats.

Wildcard

*

Represents any value.

Example:

* * * * *

Runs every minute.


Specific Value

12 * * * *

Runs at minute 12 every hour.

Example times:

01:12
02:12
03:12

Range

* 6-8 * * *

Runs every minute between 06:00 and 08:59.


Multiple Values

* * 10,15,25 * *

Runs on the 10th, 15th, and 25th of every month.


Step Values

*/8 * * * *

Runs every 8 minutes.


Cron Shorthand Schedules

Cron supports readable shortcuts.

Shortcut Equivalent


@hourly Every hour
@daily 0 0 * * *
@weekly Once per week
@monthly Once per month
@yearly / @annually Once per year
@midnight Same as @daily

Example:

@daily python manage.py update_index

Special Schedule: @reboot

Runs once when the system starts.

Example:

@reboot /path/to/script.sh

Logging Cron Job Output

Always log cron job output for debugging.

Example:

0 0 * * * /path/to/script.sh >> /path/to/log.txt

This captures:

  • standard output
  • errors

Logs are essential for troubleshooting.


Best Practice: Use Scripts

Instead of embedding complex commands directly in crontab, run scripts.

Example:

0 0 * * * /scripts/backup.sh >> /logs/backup.log

Advantages:

  • Cleaner configuration
  • Easier debugging
  • Supports multi-line logic

Email Notifications

Cron can email job output.

Add to the crontab file:

MAILTO=user@example.com

Requirements:

  • Mail server configured on the system.

Common solutions:

  • Postfix
  • Sendmail
  • ssmtp
  • Mailgun

Mailgun is often preferred due to its free tier.


Cron Access Control

Administrators can restrict cron usage using:

/etc/cron.allow
/etc/cron.deny

These files contain one username per line.

Example:

# /etc/cron.allow
userA
userB

Only these users can create cron jobs.

If cron.allow does not exist, all users are allowed unless blocked by
cron.deny.

Changes take effect immediately. Restarting the cron daemon is not
required.


Helpful Tools

Cron expressions can be difficult to construct manually.

Online generators simplify this process.

Recommended tool:

https://crontab-generator.org

Provides:

  • visual schedule builder
  • correct cron syntax generation

Summary

Cron is a reliable mechanism for automating recurring tasks in Unix
systems.

Key capabilities:

  • task scheduling
  • system maintenance
  • automated backups
  • periodic integrations
  • monitoring and reporting

For production systems:

  • log all job output
  • prefer scripts over inline commands
  • restrict cron access where necessary
  • keep schedules simple and explicit