☑️Day 13: How to Automate Tasks with Cron
🔹Table Of content :
✅Introduction
✅Key Learnings and Concepts
✅Understanding Cron and Crontab
✅Writing Basic Cron Jobs
✅Automating Disk Space Checks and Backups
✅Important Cron Commands
✅Fine-Tuning Cron Jobs
✅Takeaways from Day 13
Hey DevOps Enthusiasts,
Welcome to Day 13 ! Today’s focus was on mastering cron and crontab, which are vital for automating repetitive tasks in Linux environments. With cron jobs, you can schedule scripts, backups, and even system checks to run automatically at specific times.
✅Key Learnings and Concepts
✅1. Understanding Cron and Crontab
Cron is a time-based job scheduler in Unix-like systems, allowing you to run scripts and commands at specific times.
Crontab (Cron Table) is a file where the schedule of cron jobs is defined.
✅2. Writing Basic Cron Jobs
I created several cron jobs to automate tasks:
- Saying Hi Every Minute: I scheduled a cron job to print "Hi" every minute of every hour on the 1st and 5th of September. Here's the cron syntax for that:
* * 1,5 9 * echo "Hi" >> /home/kedar/hi_log.txt
This ensures that the message is appended to hi_log.txt
at the specified time.
✅3. Cron for Disk Space Check and Backup
I also wrote cron jobs to monitor disk space and automate backups, appending the output to a log file for future reference:
0 0 * * * df -H >> /home/kedar/check_disk_logs.txt
This script checks disk space every day at midnight and appends the results to check_disk_logs.txt
. I’ve also created a backup script that runs at specified intervals to safeguard system files.
✅4. Important Cron Commands
Mastering these commands made managing and scheduling tasks easier:
crontab -e
: Edit the crontab file to schedule tasks.crontab -l
: List all currently scheduled cron jobs.>
: Overwrite the output to a file.>>
: Append output to a file without overwriting it.
✅5. Fine-Tuning Cron Jobs
By using cron syntax, I was able to fine-tune when tasks would run, down to the minute. For example:
0 0 *: Runs the job at midnight every day.
***/10 **\: Runs the job every 10 minutes.
0 0 1 : Runs the job on the 1st day of every month
✅Takeaways from Day 13
Cron and Crontab: Cron jobs are crucial for automating repetitive tasks, from system maintenance to backups.
Logs and Backups: Scheduling jobs to monitor disk usage and automate backups keeps the system running smoothly without manual intervention.
Efficient Use of Commands: Understanding commands like
>
and>>
helped in managing logs efficiently without overwriting previous data.
🚀Stay tuned for more updates on my DevOps journey! 📅
Feel free to reach out to me if you have any questions or need any more help. Let’s connect, learn, and succeed together!
Thanks for joining me on Day 13 of this exciting journey. Let’s keep leveling up our skills!
Happy Learning!😊
#90DaysOfDevOps
#Linux #ShellScripting #SystemMonitoring #Automation #DevOpsJourney #TaskAutomation #BashScripting #CornTab #Corn #TaskScheduling
Subscribe to my newsletter
Read articles from Kedar Pattanshetti directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by