Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Scheduling Recurring Tasks in CentOS with Cron

Tech May 14 1

CentOS supports automated recurring task execution through the cron daemon, a time-based job scheduler that runs commands or scripts at predefined intervals.

Common Use Cases

  • System hygiene: Rotating or purging log files, cleraing temporary directories, verifying disk space.

  • Data protection: Executing periodic database dumps, syncing critical directories via rsync, or triggering full system snapshots.

  • Workflow automation: Launching ETL pipelines, refreshing cache layers, or polling external APIs for status updates.

  • Operational notfiications: Sending health reports via email or webhook when thresholds are exceeded.

How Cron Works

The crond service reads schedule definitions from multiple locations:

  • Per-user crontabs stored in /var/spool/cron/ (edited via crontab -e).
  • System-wide configuration in /etc/crontab, which includes an additional user field.
  • Drop-in files under /etc/cron.d/, each treated as a separate crontab with full syntax support.

Each line in a crontab follows this structure:

MIN HOUR DOM MON DOW COMMAND

Where:

  • MIN: 0–59
  • HOUR: 0–23
  • DOM: Day of month, 1–31
  • MON: Month, 1–12 or names like jan, feb
  • DOW: Day of week, 0–7 (both 0 and 7 represent Sunday) or names like mon, tue

Special characters include * (any value), , (list), - (range), and / (step values).

Setting Up a Scheduled Job

To schedule a task for the current user:

# Open the user's crontab in the default editor
crontab -e

Add a line such as:

0 2 * * 0 /usr/local/bin/weekly-backup.sh >> /var/log/backup.log 2>&1

This runs /usr/local/bin/weekly-backup.sh at 2:00 AM every Sunday.

For system-level jobs requiring root privileges, avoid editing /etc/crontab directly unless necessary. Instead, place a file in /etc/cron.d/ with proper permissions (e.g., 0644) and include the username before the command:

# /etc/cron.d/disk-check
0 6 * * * root /usr/local/sbin/check-disk-usage.sh

Ensure scripts have execute permissions (chmod +x) and use absolute paths for binaries and files.

Verify active schedules with:

crontab -l          # current user
sudo crontab -u root -l  # root's crontab
sudo systemctl status crond  # confirm service is running

Cron logs are typically found in /var/log/cron. Enable verbose logging by adjusting /etc/rsyslog.conf if deeper debugging is required.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.