Skip to main content

Command Palette

Search for a command to run...

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

Learn How To Use Cron, At, and Systemd Timers for Task Scheduling in Linux

Updated
3 min read
M

In a world where technology advances at a breakneck pace, I'm a passionate developer always inspired by the latest innovations. The revolutionary science fiction of The Matrix (1999) ignited my love for the genre and the idea of what's possible beyond the screen. When I'm not immersed in code, you can find me lost in the melodies of a piano, a peaceful counterpoint to the logic and structure of development. It’s in this harmony of technology and art that I find my true rhythm.

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

    | Tool | Purpose | Key Commands | | --- | --- | --- | | at | One-time jobs | at TIME, atq, atrm <jobid> | | cron | Recurring jobs | crontab -e, crontab -l, systemctl status cron | | anacron | Recurring jobs, catch missed | Edit /etc/anacrontab, anacron -T | | systemd timers | Modern recurring jobs | systemctl 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. 🚀

Linux

Part 5 of 10

This series is your complete guide to getting started with Linux. We'll cover core concepts, the powerful command-line interface, and how to set up your own Linux environment. Essential for anyone wanting to master the world of Linux.

Up next

How to Install and Update Software on Linux: A Guide to Package Management

Simplified Linux Software Installation and Updates: A How-To Guide