DEV Community

Cover image for Linux in Action: Mastering Crontab for Task Scheduling (RHCSA & DevOps)
DhavalThakar97
DhavalThakar97

Posted on

Linux in Action: Mastering Crontab for Task Scheduling (RHCSA & DevOps)

Introduction

Task automation is a core responsibility of every Linux system administrator. Whether you’re running nightly backups, rotating logs, generating reports, or triggering DevOps workflows, the cron scheduler provides a reliable and flexible way to execute recurring jobs.

This article provides a complete, RHCSA-aligned guide to using crontab, enriched with real-world business use cases and practical examples you can apply immediately.


Table of Contents


What Is Cron & Crontab?

cron is a built-in Linux scheduler used to run commands or scripts at specific intervals. The configuration file that defines scheduled tasks is known as the crontab.

Cron is ideal for:

  • Automated system maintenance
  • Backups and cleanup tasks
  • Log rotation and reporting
  • Scheduled data transfers
  • Compliance jobs and monitoring

Cron automates repetitive tasks without human intervention, improving consistency and reliability.


Cron Syntax Explained

Every cron entry consists of five time fields followed by the command:

MIN   HOUR   DOM   MON   DOW   CMD
Enter fullscreen mode Exit fullscreen mode

⚠️ Important: In the editor, fields must be separated using a TAB, not a spacebar.

Field meanings

Field Description Range
MIN Minute 0–59
HOUR Hour 0–23
DOM Day of month 1–31
MON Month 1–12
DOW Day of week 0–6 (Sun=0, Mon=1 … Sat=6)
CMD Command to execute

Managing the Cron Service

Check if cron service is running:

systemctl status crond
Enter fullscreen mode Exit fullscreen mode

Enable cron service at boot:

systemctl enable crond
Enter fullscreen mode Exit fullscreen mode

Restart cron service:

systemctl restart crond
Enter fullscreen mode Exit fullscreen mode

User Crontabs & Scheduling Jobs

Every user can have their own crontab.

Edit crontab as a specific user

crontab -e -u username
Enter fullscreen mode Exit fullscreen mode

Example: Schedule a job for user natasha to print a message on 15 June at 14:23

su natasha
crontab -e
Enter fullscreen mode Exit fullscreen mode

Inside the editor:

23  14  15  06  *   /usr/bin/echo hello
Enter fullscreen mode Exit fullscreen mode

Save with ESC, then :wq.

Verify:

crontab -l -u natasha
Enter fullscreen mode Exit fullscreen mode

Cron Expressions with Examples

🔹 Run a job every minute

*   *   *   *   *   /usr/bin/echo "Running every minute"
Enter fullscreen mode Exit fullscreen mode

🔹 Run a job every 5 minutes

*/5 *   *   *   *   /usr/bin/date >> /tmp/every5min.log
Enter fullscreen mode Exit fullscreen mode

🔹 Run a job every 2 minutes in the first 10 minutes

0-10/2  *   *   *   *   /usr/bin/echo "Running every 2 mins for first 10 mins"
Enter fullscreen mode Exit fullscreen mode

🔹 Run a job twice a day at 1:00 and 11:00

0   1,11    *   *   *   /usr/bin/echo "Twice a day task"
Enter fullscreen mode Exit fullscreen mode

🔹 Run a job weekdays only, between 9am–6pm

0   9-18    *   *   1-5 /usr/bin/echo "Weekday work hours task"
Enter fullscreen mode Exit fullscreen mode

Special Cron Keywords

Some schedules do not need 5 fields. Cron provides helpful shortcuts:

Keyword Equivalent Schedule
@yearly / @annually 0 0 1 1 *
@monthly 0 0 1 * *
@daily / @midnight 0 0 * * *
@hourly 0 * * * *
@weekly 0 0 * * 0
@reboot Run once after system boots

Example: Run a script that sends a New Year system status report on January 1st at 00:00 each year:

@yearly /usr/bin/echo "Happy New Year! System status report triggered." >> /var/log/newyear.log
Enter fullscreen mode Exit fullscreen mode

Running Multiple Jobs

You can schedule multiple lines in the same user's crontab:

# Run a job every day at 14:23
23  14  *   *   *   /usr/bin/echo hello

# Run a job every 5 minutes
*/5 *   *   *   *   /usr/bin/date >> /home/natasha/data.log

# Run a job every Sunday at midnight
0   0   *   *   0   /usr/bin/echo hiyaa
Enter fullscreen mode Exit fullscreen mode

Real-World Business Use Case

Scenario: Automated Reporting & Compliance in a Retail Enterprise

A retail organization needs daily sales, inventory snapshots, and error logs automatically sent to the analytics team.

Using crontab:

  • A nightly inventory export runs at 23:59.
  • A compliance log archival runs every 6 hours.
  • A backup script runs every day at 02:00.

Examples:

59  23  *   *   *   /usr/local/bin/export_inventory.sh
0   */6 *   *   *   /usr/local/bin/archive_logs.sh
0   2   *   *   *   /usr/local/bin/db_backup.sh
Enter fullscreen mode Exit fullscreen mode

Business impact:

  • Reduces manual work for IT and operations teams
  • Ensures compliance reports are always generated on time
  • Prevents data loss through automated backups
  • Improves accuracy by eliminating human errors

Cron jobs become part of the company’s automation backbone, supporting DevOps workflows, auditing, and operational reliability.


Conclusion

Crontab is one of the most powerful automation tools in Linux. With a solid understanding of cron syntax, service management, scheduling patterns, and real-world use cases, you can automate nearly any repetitive task on a Linux system.

Practice these examples, build your own automation routines, and incorporate cron into your RHCSA exam preparation and DevOps projects.

Connect with me on LinkedIn for further discussions and networking opportunities.


Top comments (0)