🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now β†’
Menu

Categories

Linux Cron Jobs: Automate Everything on Your Server (2026 Guide)

Linux Cron Jobs: Automate Everything on Your Server (2026 Guide)

Quick Summary: Cron is the built-in Linux task scheduler that runs commands automatically at specified times and intervals. Using crontab (cron table), you can schedule backups, log rotation, system updates, monitoring checks, and any repetitive task. This guide covers crontab syntax, practical scheduling examples, and modern alternatives like systemd timers.

Linux cron jobs automated task scheduling

What Is Cron?

Cron is a time-based job scheduler built into every Linux distribution. The cron daemon (crond or cron) runs in the background and checks every minute if any scheduled jobs need to execute. Jobs are defined in crontab files β€” one per user, plus system-wide crontabs.

Crontab Syntax Explained

Every cron job follows a five-field time specification:

FieldValuesDescription
Minute0-59Minute of the hour
Hour0-23Hour of the day (24h format)
Day of Month1-31Day of the month
Month1-12Month of the year
Day of Week0-7Day of the week (0 and 7 = Sunday)

Special characters: * (any value), , (list), - (range), / (step)

Practical Cron Schedule Examples

ScheduleCrontab ExpressionUse Case
Every minute* * * * *Health monitoring checks
Every 5 minutes*/5 * * * *Resource monitoring
Every hour0 * * * *Log rotation
Daily at 2 AM0 2 * * *Database backups
Weekly Sunday midnight0 0 * * 0Full system backup
Monthly 1st at 3 AM0 3 1 * *Monthly reports
Weekdays 9 AM0 9 * * 1-5Morning status reports
Every 15 min, business hours*/15 9-17 * * 1-5Business monitoring

Managing Crontab

  • crontab -e β€” Edit your personal crontab
  • crontab -l β€” List your current cron jobs
  • crontab -r β€” Remove all your cron jobs (careful!)
  • sudo crontab -e -u username β€” Edit another user's crontab

Common Cron Pitfalls and Solutions

PitfallCauseSolution
Command works manually but not in cronDifferent PATH environmentUse full paths to all commands
No output or error messagesOutput goes to mail or nowhereRedirect output: command >> /var/log/job.log 2>&1
Job runs multiple timesNo overlap preventionUse flock: flock -n /tmp/job.lock command
Timezone confusionCron uses system timezoneCheck with timedatectl, set TZ in crontab
Permission deniedScript not executablechmod +x script.sh

Cron Logging and Monitoring

  • View cron logs: grep CRON /var/log/syslog (Debian) or journalctl -u crond (RHEL)
  • Always redirect output to log files for debugging
  • Use mail for notifications: cron emails output to the user by default
  • Set MAILTO=admin@example.com at the top of crontab for email alerts

Systemd Timers: The Modern Alternative

Systemd timers offer advantages over cron for complex scheduling:

FeatureCronSystemd Timers
LoggingManual (redirect output)Built-in (journalctl)
DependenciesNoneFull dependency support
Missed jobsSkippedCan run on next boot (Persistent=true)
Randomized delayNot supportedRandomizedDelaySec
Resource limitsNot supportedFull cgroup integration
Ease of setupSimple (one line)More complex (two files)

Security Best Practices

  • Restrict crontab access with /etc/cron.allow and /etc/cron.deny
  • Use the principle of least privilege β€” run cron jobs as non-root when possible
  • Audit cron jobs regularly: for user in $(cut -f1 -d: /etc/passwd); do crontab -l -u $user 2>/dev/null; done
  • Never store passwords in crontab β€” use environment files or secret management

Frequently Asked Questions

How do I check if cron is running?

Run systemctl status cron (Debian/Ubuntu) or systemctl status crond (RHEL/AlmaLinux). The service should show "active (running)". If not, start it with systemctl start cron.

Why is my cron job not executing?

The most common reasons are: incorrect PATH (use full paths), missing execute permissions on scripts, syntax errors in the crontab (check with crontab -l), or the cron service not running. Check cron logs for error messages.

Should I use cron or systemd timers?

Use cron for simple, one-line scheduled commands. Use systemd timers when you need logging integration, dependency management, resource limits, or the ability to catch up on missed executions. Both are valid in 2026.

Can I run cron jobs every second?

No, cron's minimum interval is one minute. For sub-minute scheduling, use a loop within a cron job, systemd timers with OnUnitActiveSec=, or a dedicated process.

Related Resources

Share this article:
Dargslan Editorial Team (Dargslan)
About the Author

Dargslan Editorial Team (Dargslan)

Collective of Software Developers, System Administrators, DevOps Engineers, and IT Authors

Dargslan is an independent technology publishing collective formed by experienced software developers, system administrators, and IT specialists.

The Dargslan editorial team works collaboratively to create practical, hands-on technology books focused on real-world use cases. Each publication is developed, reviewed, and...

Programming Languages Linux Administration Web Development Cybersecurity Networking

Stay Updated

Subscribe to our newsletter for the latest tutorials, tips, and exclusive offers.