AI Reading
Quick summary of this article
Cron is a Linux utility that lets you schedule scripts or commands to run automatically at specific times or intervals in WSL Ubuntu. Each user can manage their own set of scheduled tasks, known as cron jobs, using simple commands and a specific time format.
- Use
crontab -lto view your current cron jobs andcrontab -eto edit them in a text editor. - The cron job syntax follows five fields: minute, hour, day of month, month, and day of week, followed by the command to execute.
- Example:
0 5 * * * /home/umar/backup.shruns a backup script every day at 5 AM. - Use
crontab -rto delete all your cron jobs at once. - Common uses include automating backups, log cleanup, and system updates at regular intervals.
Chapter 13: Scheduled Tasks and Cron Jobs in WSL Ubuntu
Automation is one of the most powerful features of Linux. Cron jobs allow you to schedule scripts or commands to run automatically at specified times or intervals in WSL Ubuntu.
In this chapter, you’ll learn how to view, create, and manage cron jobs for recurring tasks.
🕒 What Is Cron?
Cron is a Linux utility used to schedule commands or scripts to run periodically. Each user can have their own cron jobs.
📌 Viewing Current Cron Jobs
crontab -l
Displays all scheduled jobs for the current user.
📌 Editing Cron Jobs
crontab -e
This opens the cron table in your default text editor (nano, vim, etc.).
📅 Cron Job Syntax
# ┌──────── minute (0 - 59)
# │ ┌────── hour (0 - 23)
# │ │ ┌──── day of month (1 - 31)
# │ │ │ ┌── month (1 - 12)
# │ │ │ │ ┌ day of week (0 - 6) (Sunday=0)
# │ │ │ │ │
# * * * * * command_to_execute
Examples:
- Run a script every day at 5 AM:
0 5 * * * /home/umar/backup.sh - Run a command every 15 minutes:
*/15 * * * * /home/umar/check_logs.sh - Run a script every Monday at 2 PM:
0 14 * * 1 /home/umar/weekly_report.sh
📌 Managing Cron Jobs
Delete All Cron Jobs
crontab -r
View Cron Jobs for Another User
sudo crontab -u username -l
Edit Cron Jobs for Another User
sudo crontab -u username -e
🧪 Practical Exercises
# Schedule a backup script every day at midnight
0 0 * * * /home/umar/backup.sh
# Schedule a log cleanup script every 2 hours
0 */2 * * * /home/umar/cleanup_logs.sh
# Schedule a system update every Sunday at 3 AM
0 3 * * 0 sudo apt update && sudo apt upgrade -y
✅ Summary
- Cron allows automation of commands and scripts at scheduled times
crontab -l– View current cron jobscrontab -e– Edit cron jobscrontab -r– Remove all cron jobs- Use proper syntax to define minute, hour, day, month, and day of week
- Automate backups, maintenance, updates, and repetitive tasks
▶️ Coming Up Next
Chapter 14 – Package Management in WSL Ubuntu
Learn how to install, update, and remove software packages using apt and manage repositories effectively.
