Welcome to Day 7 of the 30 Days of Linux Challenge. Today’s topic is a game-changer when it comes to productivity and system management: automating tasks using cron
and systemd timers
.
If you've ever wanted to schedule backups, cleanup scripts, or periodic monitoring tools — this is how you do it in Linux.
📚 Table of Contents
- Why Task Scheduling Matters
- Getting Started with Cron
- Crontab Syntax and Examples
- Using systemd Timers (Modern Alternative)
- Try It Yourself
- Why This Matters
Why Task Scheduling Matters
Task automation is at the heart of efficient system management. Instead of manually running scripts every day or reminding yourself to clear logs every week, Linux allows you to automate these actions using cron
or systemd timers
.
These tools help you:
- Save time
- Reduce human error
- Maintain system hygiene
- Handle routine work automatically
Getting Started with Cron
What is Cron?
Cron is a time-based job scheduler built into Unix-like systems. A scheduled task in cron is called a cron job.
Every user can have their own crontab file (short for "cron table") where they define what should run and when.
View your crontab:
crontab -l
Edit your crontab:
crontab -e
This opens the crontab file in your default text editor (usually nano or vim).
Crontab Syntax and Examples
Each cron job follows a specific format:
┌──────── minute (0 - 59)
│ ┌────── hour (0 - 23)
│ │ ┌──── day of month (1 - 31)
│ │ │ ┌── month (1 - 12)
│ │ │ │ ┌─ day of week (0 - 6) (Sunday = 0)
│ │ │ │ │
* * * * * command_to_run
- Example 1: Run a backup script every day at 2 AM
0 2 * * * /home/user/scripts/backup.sh
- Example 2: Run a cleanup every 10 minutes
*/10 * * * * /home/user/scripts/cleanup.sh
You can redirect output to a log file like this:
*/10 * * * * /home/user/scripts/cleanup.sh >> /home/user/cleanup.log 2>&1
Using systemd Timers (Modern Alternative)
Why use systemd timers?
- Integrated with systemctl
- Logs activity using journalctl
- Supports calendar syntax like daily, hourly, or specific times
- Persistent timers survive system restarts
systemd Timer Setup Requires Two Files:
A .service file that defines what to run
A .timer file that defines when to run it
Step 1: Create the Service Unit
/etc/systemd/system/backup.service
ini
[Unit]
Description=Run daily backup script
[Service]
ExecStart=/home/user/scripts/backup.sh
Step 2: Create the Timer Unit
/etc/systemd/system/backup.timer
ini
[Unit]
Description=Daily backup schedule
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
Enable and Start the Timer
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
View Active Timers
systemctl list-timers
This method gives you detailed logging and fine-grained control over system tasks.
Try It Yourself
Create a sample script
echo "echo Hello from cron at \$(date)" > ~/hello.sh
chmod +x ~/hello.shAdd a cron job that runs every 2 minutes:
crontab -e
Add:
*/2 * * * * /home/youruser/hello.sh >> /home/youruser/hello.log 2>&1
- Create a systemd timer and service (optional) If you’re feeling confident, replicate the example above using your own script!
Why This Matters
In real-world sysadmin and DevOps roles, task scheduling is everywhere:
- Automated backups
- Log rotation
- Scheduled alerts or reports
- Regular updates and patching
- Running system health checks
- Mastering cron and systemd timers ensures that your systems run smoothly, even when you're not watching.