One of the best things about Linux? It loves automation.
Whether you want to schedule backups, send yourself a daily weather report, or clean up log files every night—cron jobs and bash scripts are your best friends.
In this guide, we’ll walk you through the basics of Linux task automation, show you how to write simple bash scripts, and schedule them using cron.
What Are Cron Jobs?
A cron job is a scheduled task that runs automatically at a specified time or interval using the cron daemon.
Think of it like a calendar app for your system that never forgets.
Example use cases:
- Back up files every night at 2 AM
- Reboot a server once a week
- Clean up temp folders daily
- Fetch new data from APIs on a schedule
Step 1: Write a Simple Bash Script
Let’s start with a basic task: creating a backup of a folder.
Example: backup.sh
!/bin/bash
SOURCE=”/home/yourusername/Documents”
DEST=”/home/yourusername/Backups”
DATE=$(date +%Y-%m-%d)
mkdir -p “$DEST”
tar -czf “$DEST/backup-$DATE.tar.gz” “$SOURCE”
Make it executable:
chmod +x backup.sh
That’s it—you just wrote your first automation script.
Step 2: Schedule Your Script with Cron
To schedule tasks, you edit the crontab (cron table) for your user:
crontab -e
Cron syntax:
* * * * command-to-run
│ │ │ │ │
│ │ │ │ └── Day of the week (0 – 7) [Sunday is 0 or 7]
│ │ │ └──── Month (1 – 12)
│ │ └────── Day of month (1 – 31)
│ └──────── Hour (0 – 23)
└────────── Minute (0 – 59)
Example: Run backup.sh every day at 2 AM
0 2 * * * /home/yourusername/scripts/backup.sh
Example: Run script every 10 minutes
*/10 * * * * /home/yourusername/scripts/task.sh
Step 3: Test and Monitor
Cron jobs don’t show output unless you tell them to.
Add logging to your crontab entry:
0 2 * * * /home/yourusername/scripts/backup.sh >> /home/yourusername/cron.log 2>&1
>>appends output to the log file2>&1captures errors
View logs:
tail -f /home/yourusername/cron.log
Helpful Commands
- List your current cron jobs:
crontab -l
- Remove all your cron jobs:
crontab -r
- Use system-wide cron jobs: Edit files in
/etc/crontabor/etc/cron.d/(admin-level access)
Pro Tips
- Use absolute paths in your scripts. Cron doesn’t know your usual environment.
- Test scripts manually before scheduling.
- Avoid overlapping jobs—use
flockor PID locks if needed. - Use
cron.hourly,cron.daily, etc., for system-managed schedules (/etc/cron.daily/etc.)
Real-World Automation Examples
Clean old log files every week:
!/bin/bash
find /var/log -type f -name “*.log” -mtime +7 -delete
Crontab:
0 3 * * 0 /home/yourusername/scripts/clean_logs.sh
Check internet connectivity every 5 minutes:
!/bin/bash
ping -c 1 google.com > /dev/null || echo “$(date): No internet” >> ~/ping_failures.log
Auto-update your system every night:
!/bin/bash
apt update && apt upgrade -y
(Make sure you’re okay with unattended updates before using this on critical systems.)
Linux was built for automation—and once you learn how to use cron and bash, you can make your machine work for you 24/7.
Even small scripts can save you hours over time. So whether you’re backing up data, maintaining a server, or just geeking out, automation is a superpower worth learning.
