็†ฑ้–€ๅˆ†้กž
 ่ผ‰ๅ…ฅไธญ…
็›ฎ้Œ„

๐Ÿง Linux Crontab & System Scheduling Complete Guide: Cron & systemd-timer Practical Tutorial

    ๐Ÿง Linux Crontab & System Scheduling Complete Guide: Cron & systemd-timer Practical Tutorial

    Task scheduling is a fundamental component of Linux system administration. This guide provides a complete and practical explanation of crontab, cron daemon, and systemd timers. With real-world examples, you will learn how to automate backups, health checks, log cleanup, and more.

    ๐Ÿ“Œ 1. Overview of Cron and Crontab

    Cron is the system scheduler that runs tasks at specified times. Crontab is the configuration file where users define their scheduled jobs.

    • cron: the background service that triggers tasks.
    • crontab: per-user or system-wide schedule definitions.
    • Locations:
      • /etc/crontab
      • /etc/cron.d/, /etc/cron.hourly/, etc.
      • crontab -e for user-specific schedules

    ๐Ÿ“ 2. Crontab Format (5 Fields)

    The standard crontab syntax includes 5 fields:

    # Min   Hour   Day   Month   Weekday
    *      *      *      *       *     command
    
    FieldValuesDescription
    Minute0–59Minute of the hour
    Hour0–23Hour of the day
    Day1–31Day of the month
    Month1–12Month
    Weekday0–70/7 = Sunday

    ✔ Common symbols

    *   all values  
    ,   list of values (e.g., 1,3,5)  
    -   range (e.g., 1-5)  
    /   step value (e.g., */10 = every 10 minutes)
    

    ๐Ÿ“˜ 3. Practical Crontab Examples

    1️⃣ Daily backup at 02:00

    0 2 * * * /usr/local/bin/backup.sh
    

    2️⃣ Run health check every 10 minutes

    */10 * * * * systemctl is-active nginx >> /var/log/nginx_health.log
    

    3️⃣ Weekly cleanup of logs older than 7 days

    0 3 * * 1 find /var/log -type f -mtime +7 -delete
    

    ๐Ÿ“Œ Check cron execution logs

    journalctl -u cron
    # or
    grep CRON /var/log/syslog
    

    ๐Ÿงฉ 4. Systemd Timers: Modern Replacement for Cron

    For modern Linux systems (CentOS 7+, RHEL, Ubuntu 18+), systemd timers are recommended. They provide better logging, dependency control, stronger reliability, and more flexible scheduling.

    ๐Ÿ”ฅ 5. Creating a Systemd Service + Timer

    1️⃣ Create the Service

    # /etc/systemd/system/cleanup.service
    [Unit]
    Description=Cleanup old logs
    
    [Service]
    Type=oneshot
    ExecStart=/usr/local/bin/cleanup.sh
    

    2️⃣ Create the Timer

    # /etc/systemd/system/cleanup.timer
    [Unit]
    Description=Run cleanup job daily
    
    [Timer]
    OnCalendar=daily
    Persistent=true
    
    [Install]
    WantedBy=timers.target
    

    3️⃣ Enable the timer

    systemctl daemon-reload
    systemctl enable --now cleanup.timer
    systemctl status cleanup.timer
    

    ๐Ÿ“Œ Check execution logs

    journalctl -u cleanup.service
    systemctl list-timers
    

    ⚖️ 6. Cron vs Systemd Timer Comparison

    FeatureCronSystemd Timer
    Loggingsyslog onlyjournalctl full logs
    StabilityBasicHigh; supports dependencies
    Complex tasksHarderEasier and scalable
    System integrationLowFull systemd integration

    Recommendation: Use systemd timers for modern systems.

    ๐Ÿ”ง 7. Troubleshooting

    1️⃣ Cron not running?

    systemctl status cron
    crontab -l
    grep CRON /var/log/syslog
    

    2️⃣ Commands not executing (PATH problems)

    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
    # Add this at the top of your crontab
    

    3️⃣ Timer not triggering?

    systemctl status yourjob.timer
    systemctl list-timers
    journalctl -u yourjob.service
    

    ๐Ÿ“˜ Conclusion

    Cron and systemd timers are essential components of Linux automation. Cron is simple and effective for lightweight tasks, while systemd timers offer stability, dependency control, and better observability for modern deployments. For new environments, systemd timers are highly recommended for long-term maintainability.


    ๐Ÿ”— Related Reading

    — WWFandy・Linux Automation Notes

    ๐Ÿ”— ๅˆ†ไบซ้€™็ฏ‡ LINE Facebook X

    ๆฒ’ๆœ‰็•™่จ€:

    ๅผต่ฒผ็•™่จ€

    ๅญ—็ดš