Scheduling with cron, at, and Timers¶
Overview¶
Backups, reports, and cleanup need reliable schedules with visible logs.
This is Tutorial 17 in Module 11: Scheduling & Automation of the REBASH Academy Linux for Cloud & DevOps Engineers series — written for administrators, DevOps engineers, SREs, and platform engineers operating production Linux.
Prerequisites¶
- Package Management
- Terminal access with a regular user account (sudo where noted)
Learning Objectives¶
By the end of this tutorial, you will be able to:
- Apply the core ideas of “Scheduling with cron, at, and Timers” on a real Linux host
- Use modern tools (
ip/ss,systemctl/journalctl) where they apply - Complete the lab under
~/rebash-linux/with clear outputs - Relate this topic to Cloud, DevOps, and production operations
- Explain the failure modes you would check first in an incident
Architecture¶
Linux ops work sits between humans/automation and the kernel, services, and network. This topic’s control points are shown below.
Theory¶
What it is¶
Linux schedules deferred and recurring work with cron (recurring calendar tables), at (one-shot jobs), and systemd timers (unit-based schedules). Cron entries set minute, hour, day of month, month, and day of week plus a command. Timers activate associated .service units using calendar or monotonic expressions and integrate with the journal and dependency graph.
Why it matters¶
Backups, certificate renewals, report scrapes, and housekeeping scripts fail silently when the scheduler environment differs from your interactive shell — especially PATH and working directory. Choosing timers for systemd-managed software improves observability; keeping cron for simple per-user tasks remains valid. Missed jobs are a common root cause of “stale data” incidents.
How it works¶
crontab -e edits the current user’s table; system-wide jobs live in /etc/crontab and /etc/cron.d/. Always use absolute paths or set PATH in the crontab. Redirect stdout/stderr to a log file — cron mail is easy to miss. at queues a command for a future time (atq/atrm manage the queue). For timers, create a service that runs the work and a timer that triggers it; systemctl enable --now foo.timer starts the schedule; systemctl list-timers shows next/last run. Prefer timers when you need randomised delay, network-online ordering, or unified failure logs via journalctl -u.
Key concepts and comparisons¶
| Mechanism | Best for |
|---|---|
| User crontab | Personal or per-account recurring jobs |
/etc/cron.d/ | Package- and system-shipped jobs |
at | One-off deferred commands |
| systemd timer | Services needing deps, journal, jitter |
| Field (cron) | Meaning |
|---|---|
| minute hour dom month dow | When to run |
| command | What to run (absolute path preferred) |
Common pitfalls¶
- Relying on interactive
PATH— cron’s environment is minimal. - Enabling the
.serviceinstead of the.timer(or vice versa). - Omitting log redirection and assuming “no mail” means success.
- Running jobs as root unnecessarily; prefer a dedicated service user.
- Overlapping long jobs every minute without locking (
flock).
Hands-on Lab¶
Create a workspace for this tutorial.
Focus: add a user crontab entry; queue an at job; inspect systemd timers
Step 1 – Schedule safely¶
crontab -l 2>/dev/null | tee crontab-before.txt || true
echo "# lab only — remove after class" > cron.line
echo "*/30 * * * * date >> $HOME/rebash-linux/lab17/cron-tick.log" >> cron.line
cat cron.line
systemctl list-timers --all 2>/dev/null | head | tee timers.txt || true
command -v at && echo 'echo lab-at | at now + 1 minute' || echo 'at not installed'
Final step – Cleanup note¶
Validation¶
- Lab commands run under
~/rebash-linux/lab17/ - You can explain each Theory bullet in your own words
- You used modern tooling where applicable (
ip/ss,systemctl/journalctl) - You can describe one production failure mode for this topic
Code Walkthrough¶
Production Linux practice for Scheduling with cron, at, and Timers always combines:
- Inspect before you change (
status,df,ip, logs) - Prefer reversible, documented changes (config management, drop-ins)
- Capture evidence (command output, journal snippets) for handovers
- Prefer
systemctl/journalctlandip/ssover legacy tools - Least privilege — escalate with
sudoonly when required
Keep runbooks short enough to follow at 03:00. Automate the boring checks; keep humans for judgement.
Security Considerations¶
- Treat host access and sudo as privileged — audit who can do what
- Never paste secrets into shell history, tickets, or screenshots
- Validate device names and paths before destructive disk or
rmoperations - Prefer key-based SSH and deny password auth on internet-facing hosts
- Collect logs centrally; restrict who can read authentication and audit trails
Common Mistakes¶
Using legacy networking tools by default
ifconfig/netstat are missing or incomplete on modern images. Fix: use ip and ss.
Editing vendor unit files in place
Package upgrades overwrite /lib/systemd/system. Fix: systemctl edit drop-ins under /etc.
Trusting df without checking inodes and mounts
A full /var or exhausted inodes looks different from root. Fix: df -h, df -i, and findmnt.
Best Practices¶
- Golden images + config as code over snowflake hosts
- Alert on symptoms (failed units, disk, load) with runbooks attached
- Time-sync (chrony) everywhere — logs and TLS depend on it
- Separate OS and data volumes on Cloud VMs
- Practise restore and rescue paths before you need them
Troubleshooting¶
| Symptom | Likely cause | Fix |
|---|---|---|
| Permission denied | Mode/owner/ACL/MAC | namei -l, id, getfacl, SELinux/AppArmor logs |
| No route / timeout | Routing, DNS, firewall | ip route, dig, ss, security groups |
| Service won’t start | Unit/config/deps | systemctl status, journalctl -u, config -t |
| Disk full | Logs, containers, deleted-open | df/du, lsof +L1, rotate/expand |
| High load | CPU, I/O wait, thrash | vmstat, iostat, ps |
Summary¶
Scheduling with cron, at, and Timers is essential for Cloud and DevOps engineers operating Linux hosts. Practise the lab until the inspection path is muscle memory, then continue the track.
Interview Questions¶
- How does this topic show up when operating Cloud VMs or Kubernetes nodes?
- What would you check first if this area misbehaves in production?
- Which modern Linux tools replace older equivalents here?
- What security control should accompany this capability?
- How would you automate verification of this topic in CI or a cron/timer job?
Sample answer — question 2
Start with blast radius and recent changes, then gather host signals (systemctl --failed, df, ip/ss, journalctl) before making changes. Fix forward with evidence, not guesswork.
Related Tutorials¶
- Linux for Cloud & DevOps – Category Overview
- Package Management (previous)
- Logging — syslog, journald, and logrotate (next)
- Learning Paths