← Back to Blog

20 Cron Job Examples for Linux: Schedule Any Task (2026)

Cron is one of the most powerful and most misunderstood tools on any Linux server. From simple daily backups to complex multi-step pipelines, mastering cron expressions will save you hours every week. This guide covers the syntax, 30+ real-world examples, common mistakes, and a step-by-step setup walkthrough.

The Problem: Manual Task Execution Does Not Scale

Every production system has tasks that need to run on a schedule: database backups, log rotation, cache warming, report generation, certificate renewal, data imports, health checks. If you are running these by hand or relying on developers to remember them, you have a reliability problem waiting to happen.

Cron - the Unix job scheduler built into every Linux and macOS system - solves this. It has been around since 1975, is installed everywhere, requires zero dependencies, and is reliable enough that the entire internet trusts it with critical infrastructure tasks. The barrier for most developers is not cron itself but the expression syntax, which looks cryptic at first glance.

This guide demystifies that syntax completely, with real examples you can copy and use today.

Understanding the Crontab Syntax

A cron expression consists of five time fields followed by the command to execute:

# ┌───────── minute (0–59)
# │ ┌───────── hour (0–23)
# │ │ ┌───────── day of month (1–31)
# │ │ │ ┌───────── month (1–12 or JAN–DEC)
# │ │ │ │ ┌───────── day of week (0–7, 0 and 7 = Sunday, or SUN–SAT)
# │ │ │ │ │
# * * * * *  command to execute

The five special characters you need to know:

  • * - every value (wildcard)
  • */n - every n-th value (step)
  • a,b - specific values a and b (list)
  • a-b - range from a to b
  • @reboot, @daily, etc. - named shortcuts

30 Real-World Cron Job Examples

Every Minute and Every Few Minutes

ExpressionDescription
* * * * *Every minute (health checks, queue workers)
*/2 * * * *Every 2 minutes
*/5 * * * *Every 5 minutes (API polling, metrics collection)
*/10 * * * *Every 10 minutes
*/15 * * * *Every 15 minutes (cache warm-up)
*/30 * * * *Every 30 minutes

Hourly and Every Few Hours

ExpressionDescription
0 * * * *Every hour at minute 0 (hourly reports)
0 */2 * * *Every 2 hours
0 */4 * * *Every 4 hours
0 */6 * * *Every 6 hours (incremental backups)
0 */12 * * *Twice a day (every 12 hours)
30 9 * * *Daily at 9:30 AM (morning report)

Daily, Weekly, and Monthly

ExpressionDescription
0 0 * * *Daily at midnight (database backup)
0 2 * * *Daily at 2 AM (off-peak maintenance)
0 9 * * 1-5Weekdays at 9 AM (business day tasks)
0 0 * * 0Weekly on Sunday at midnight
0 0 * * 1Weekly on Monday at midnight
0 0 1 * *Monthly on the 1st at midnight
0 0 15 * *Monthly on the 15th (billing runs)
0 0 1 1 *Yearly on January 1st (annual reports)

Business Hours and Complex Schedules

ExpressionDescription
*/15 9-17 * * 1-5Every 15 min during business hours (Mon–Fri, 9–5)
0 8,12,17 * * 1-5At 8 AM, noon, and 5 PM on weekdays
0 0 * * 6,0Weekends at midnight
0 6-22 * * *Every hour between 6 AM and 10 PM
0 0 1,15 * *1st and 15th of every month
0 0 * * 1#1First Monday of every month (some cron variants)

Named Shortcuts (@reboot, @daily, etc.)

ShortcutEquivalentUse case
@rebootOn startupStart services, mount drives, init scripts
@yearly / @annually0 0 1 1 *Annual maintenance tasks
@monthly0 0 1 * *Monthly cleanup or billing
@weekly0 0 * * 0Weekly database optimization
@daily / @midnight0 0 * * *Daily backups and log rotation
@hourly0 * * * *Hourly report generation

Step-by-Step: Setting Up Your First Cron Job

Step 1: Open the Crontab Editor

crontab -e

This opens your user's crontab file in the default editor (usually nano or vi). To edit the system-wide crontab (for root tasks), use sudo crontab -e.

Step 2: Write Your Cron Expression

Add a line with your schedule and command. Always use absolute paths because cron runs with a minimal environment and does not know your PATH:

# Bad - cron may not find 'python3' in its PATH
0 2 * * * python3 /home/ubuntu/backup.py

# Good - use the full path to the binary
0 2 * * * /usr/bin/python3 /home/ubuntu/backup.py

# Even better - redirect output to a log file
0 2 * * * /usr/bin/python3 /home/ubuntu/backup.py >> /var/log/backup.log 2>&1

Step 3: Redirect Output

By default, cron emails output to the system user. Redirect to a file to capture logs:

# Capture both stdout and stderr
0 2 * * * /path/to/script.sh >> /var/log/myjob.log 2>&1

# Discard all output (silent mode)
0 2 * * * /path/to/script.sh > /dev/null 2>&1

Step 4: Verify the Crontab Was Saved

crontab -l

This lists all cron jobs for the current user. To view another user's crontab: sudo crontab -l -u username.

Step 5: Check the Cron Daemon Is Running

# Ubuntu/Debian
sudo systemctl status cron

# CentOS/RHEL
sudo systemctl status crond

# View recent cron executions in the system log
grep CRON /var/log/syslog | tail -20

Generate Cron Expressions Visually

Not sure about your cron syntax? Our free Crontab Generator lets you build expressions using dropdowns and instantly see a human-readable description. No memorization required.

Open Crontab Generator

Real-World Cron Job Examples with Full Commands

Daily Database Backup (MySQL)

# Every day at 2 AM - backup MySQL database to /backups/
0 2 * * * /usr/bin/mysqldump -u root -p'yourpassword' mydb | gzip > /backups/mydb_$(date +\%Y\%m\%d).sql.gz 2>> /var/log/backup.log

Weekly Log Rotation

# Every Sunday at 3 AM - compress logs older than 7 days
0 3 * * 0 find /var/log/myapp -name "*.log" -mtime +7 -exec gzip {} \; 2>/dev/null

Let's Encrypt Certificate Renewal

# Twice daily - certbot checks if renewal is needed (only renews if <30 days remaining)
0 */12 * * * /usr/bin/certbot renew --quiet --post-hook "systemctl reload nginx"

Clear Application Cache at Midnight

# Daily at midnight - flush Redis cache
0 0 * * * /usr/bin/redis-cli FLUSHDB >> /var/log/cache-flush.log 2>&1

Health Check Every 5 Minutes

# Every 5 minutes - ping API and log result
*/5 * * * * curl -sf https://api.example.com/health >> /var/log/healthcheck.log 2>&1 || echo "$(date): API DOWN" >> /var/log/healthcheck-alerts.log

Send a Report Every Monday Morning

# Every Monday at 8 AM
0 8 * * 1 /usr/bin/python3 /opt/scripts/weekly_report.py | mail -s "Weekly Report" team@example.com

Common Cron Mistakes and How to Avoid Them

Mistake 1: Relative Paths

Cron runs commands with a minimal environment. The PATH variable only includes /usr/bin:/bin. Always use full absolute paths for both the binary and any files it references.

Mistake 2: Percent Signs in Commands

The % character has special meaning in crontab - it is treated as a newline. Escape it with a backslash: \%. This is most common with date +\%Y\%m\%d.

Mistake 3: Wrong Timezone

Cron runs in the system timezone, not your user timezone. On servers in UTC, 0 9 * * * means 9 AM UTC, which may not be 9 AM in your local time. To set a per-crontab timezone, add CRON_TZ=America/New_York at the top of the crontab file.

Mistake 4: No Output Logging

If your cron job silently fails, you will never know. Always redirect output: command >> /var/log/myjob.log 2>&1. The 2>&1 redirects stderr to stdout so errors are captured in the same file.

Mistake 5: Overlapping Executions

If a job takes longer than its interval, multiple instances will run simultaneously. Use flock to prevent this:

# Only one instance at a time
*/5 * * * * flock -n /tmp/myjob.lock /path/to/script.sh

Mistake 6: Running as Root When Not Needed

Use the least-privileged user for each job. If the task only reads files in /var/www, run it as www-data, not root. System crontab files in /etc/cron.d/ let you specify the user:

# /etc/cron.d/myapp
0 2 * * * www-data /usr/bin/php /var/www/html/artisan schedule:run

Cron vs. Alternatives: When to Use What

Cron is the right tool when you need a simple, reliable, OS-level scheduler with no dependencies. But modern infrastructure sometimes calls for alternatives:

  • Kubernetes CronJob: For containerized workloads. Handles retries, parallelism, history limits, and resource limits natively.
  • Celery Beat (Python): Application-level scheduling with dynamic schedules stored in a database. Integrates with Celery workers.
  • GitHub Actions scheduled workflows: CI/CD jobs that run on a cron schedule. Good for repository maintenance tasks.
  • AWS EventBridge Scheduler: Serverless, highly reliable, supports one time and recurring schedules. No server required.
  • Systemd timers: Modern replacement for cron on systemd-based Linux. Better logging, dependency management, and failure handling.

For most server side tasks on a single machine - backups, log rotation, report generation - plain cron is the simplest and most reliable choice. Reach for alternatives only when you need distributed execution, retries, or dynamic schedule management.

FAQ

What does */5 * * * * mean?

It runs the command every 5 minutes. The */5 in the minute field means "every value divisible by 5" - so at 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, and 55 minutes past the hour, every hour, every day.

How do I run a cron job at a specific time zone?

Add CRON_TZ=America/New_York (or any valid IANA timezone) at the top of your crontab file. All jobs below that line will use the specified timezone. This is a GNU cron extension - check that your system's cron supports it with man 5 crontab.

How do I run a cron job on reboot?

Use the @reboot special string: @reboot /path/to/script.sh. This runs once when the system starts. It is commonly used to start services, initialize application state, or run startup health checks.

Why is my cron job not running?

The most common reasons: (1) wrong path - use absolute paths for all binaries; (2) permission issue - the cron user cannot execute the script; (3) cron daemon not running - check with systemctl status cron; (4) syntax error in crontab - validate with an online tool; (5) output going nowhere - add 2>&1 >> /tmp/cron.log to capture errors.

What is the difference between /etc/crontab, crontab -e, and /etc/cron.d/?

crontab -e edits per-user crontabs stored in /var/spool/cron/. /etc/crontab is the system crontab and includes a username field after the time expression. /etc/cron.d/ is a directory of drop-in crontab files, also with a username field - used by packages to install their own scheduled tasks without modifying /etc/crontab directly.

Can cron run a job less often than every minute?

Cron's minimum resolution is one minute. For sub-minute scheduling, you have two options: (1) run a script every minute that checks whether it should execute based on elapsed time; (2) use a different tool like systemd timers (which support second-level resolution) or an application-level scheduler.

Quick Reference: Cron Field Summary

Field         Allowed values    Special chars
-----------   ---------------   ----------------
Minute        0–59              * , - /
Hour          0–23              * , - /
Day of month  1–31              * , - / ?
Month         1–12 or JAN–DEC   * , - /
Day of week   0–7 or SUN–SAT    * , - / ?
              (0 and 7 = Sunday)

Use our free tool here → Crontab Generator - build and validate any cron expression visually, with an instant plain-English description of the schedule.

UK
Written by Usman Khan
DevOps Engineer | MSc Cybersecurity | CEH | AWS Solutions Architect

Usman has 10+ years of experience securing enterprise infrastructure, managing high-traffic servers, and building zero-knowledge security tools. Read more about the author.