Marwan Ayman Shawky

Cloud & DevOps Engineer

Scheduling & Automation in Linux: Mastering Cron, At, and Systemd Timers

·Calculating...·
linuxautomation

One of the most powerful features of Linux is the ability to automate tasks. Whether it’s running a backup every night, sending reports every week, or cleaning up logs at boot, scheduling saves time and prevents human error.

Linux offers multiple ways to schedule tasks:

  • at → run a job once at a specific time

  • cron → run jobs repeatedly on a schedule

  • anacron → run cron-like jobs that may have been missed (good for laptops)

  • systemd timers → the modern replacement/enhancement to cron

This guide covers all four, with examples, cheatsheets, and practice exercises.

1. One-Time Jobs with at

The at command schedules a job to run once in the future.

Enable the service (CentOS/WSL2)

sudo systemctl start atd
sudo systemctl enable atd

Usage

echo "echo 'Hello World' >> /tmp/at_test.log" | at now + 2 minutes
  • now + 2 minutes → natural language time format

  • Jobs go into a queue (atq, atrm <jobid>)

Missed Jobs with anacron

cron assumes your system is always on. If the system is off, jobs are skipped. anacron solves this by running jobs once the system is back online.

  • Config file: /etc/anacrontab

  • Syntax:

    period   delay   job-identifier   command
    
  • Example:

    1   10   daily_cleanup   /home/user/cleanup.sh
    

    → Run once daily, wait 10 minutes after boot.

4. Modern Scheduling with systemd Timers

Most modern distros (CentOS 7+, Ubuntu 16+) use systemd. Instead of cron, you can use timers.

Example: Daily Job

  1. Service file /etc/systemd/system/myjob.service

    [Unit]
    Description=My Custom Job
    
    [Service]
    ExecStart=/home/user/backup.sh
    
  2. Timer file /etc/systemd/system/myjob.timer

    [Unit]
    Description=Run backup script daily
    
    [Timer]
    OnCalendar=daily
    Persistent=true
    
    [Install]
    WantedBy=timers.target
    
  3. Enable & Start

    sudo systemctl enable --now myjob.timer
    systemctl list-timers
    

    Cheatsheet: Quick Scheduling Commands

    ToolPurposeKey Commands
    atOne-time jobsat TIME, atq, atrm <jobid>
    cronRecurring jobscrontab -e, crontab -l, systemctl status cron
    anacronRecurring jobs, catch missedEdit /etc/anacrontab, anacron -T
    systemd timersModern recurring jobssystemctl list-timers, .timer units

    Practice Exercises

    🟢 Beginner

    1. Schedule a job with at that writes "Take a break!" into /tmp/reminder.log after 1 minute.

    2. Create a cron job that runs every 5 minutes and writes the current date into /tmp/date.log.

    🟡 Intermediate

    1. Use anacron to run a cleanup script daily, but delay execution for 15 minutes after boot.

    2. Write a cron job that runs only on Mondays at 9:30 AM.

    🔵 Advanced

    1. Create a systemd timer that runs echo "Systemd Rocks" into /tmp/systemd.log every hour.

    2. Compare the execution of a cron job vs. a systemd timer — which one provides better logging?

Close-up

update, and manage packages — the building blocks of your Linux environment. Without package management, automation has nothing to run.

That’s why in the next article, we’ll dive into: 📦 Linux Package Management — exploring apt, yum/dnf, rpm, repositories, and how to keep your system lean, secure, and up to date.

Stay tuned — because mastering package management is your next step toward becoming a confident Linux Admin. 🚀