Table of Contents
- Introduction
- What is Task Scheduling in Linux?
- Core Cron Commands
- Common Cron Expressions and Their Use Cases
- Real-World Scenario: Automating Cron Job
- Conclusion
- Let's Connect
Introduction
Welcome back to Day 13 of the Linux Challenge.
We have now explored user management, permissions, and process management. Today, we move into something every serious sysadmin, DevOps engineer, and cloud platform team uses: automation.
Let’s be real, nobody wants to manually run a cleanup script at 1 AM. Now that’s where scheduling comes in.
What is Task Scheduling in Linux?
Task scheduling in Linux is how we automate repetitive or time-based operations. Whether it is:
- Backing up logs every night
- Clearing a temp folder every week
- Triggering a script after deployment
This is done using cron for recurring tasks and at for one-time jobs.
Core Cron Commands
Command | Description |
---|---|
cron |
For running repeating tasks |
crontab |
Where you list those scheduled tasks |
at |
For one-time scheduled tasks |
crontab -e |
Edit your scheduled tasks |
crontab -l |
View current cron jobs |
crontab -r |
Remove all scheduled cron jobs |
at TIME |
Schedule a one-time job |
`echo "command" | at TIME` |
atq |
View all at jobs |
atrm <job number> |
Delete an at job |
systemctl status crond |
See if cron service is running |
systemctl restart crond |
To restart cron service |
MAILTO="email" |
Get cron output via email |
grep CRON /var/log/cron |
View cron logs (for debugging) |
*/2 * * * * |
Every 2 minutes cron syntax example |
Common Cron Expressions and Their Use Cases
Cron Expression | Meaning | Example Use Case |
---|---|---|
* * * * * |
Every minute | Run a health check script every minute |
*/10 * * * * |
Every 10 minutes | Auto-sync logs every 10 mins |
0 * * * * |
Every hour | Check and rotate logs hourly |
0 2 * * * |
At 2:00 AM daily | Perform system backup |
0 9 * * 1-5 |
At 9:00 AM, Monday to Friday | Send out daily team reports |
0 0 1 * * |
At midnight on the 1st of the month | Generate monthly invoice or cleanup temp |
30 21 * * 5 |
At 9:30 PM every Friday | Weekly audit or summary email |
15 14 1 * * |
At 2:15 PM on the 1st of the month | Send performance metrics report |
Real-World Scenario: Automating Cron Job
Let's schedule a simple cron job by appending the text "2 minutes of job done" every 2 minutes.
- Check if cron is running:
sudo systemctl status crond
- Create a script file and a txt file for this cron job
bash
touch /democronjobs.txt
vim /demojob.sh >> /democronjob.txt
# Add '2 minutes of cron demo job done' inside the script.
- Add Execution permission
We will change the permission mode of the script and assign it an execution mode to run.
- Schedule a Repeating Job Using crontab
bash
crontab -e
*/2 * * * * /demojob.sh - # add this inside
crontab -l - #to check it
Note
*/2 * * * * /demojob.sh
This expression means: run the /demojob.sh script every 2 minutes of every hour, every day
- Let's check if this is running
The script is meant to add one line every 2 minutes.
bash
cat /democronjobs.txt
- Stopping a Cron Job
We will simply open the crontab -e
and delete the line.
bash
crontab -e
# delete the line or comment it using #
crontab -l - to verify
Alternatively: You can disable the cron job temporarily
systemctl stop crond - to temporarily stop
systemctl start crond - to start it again
Conclusion
Whether you are scheduling backups, cleaning up logs, or monitoring server health, cron jobs save you time and keep your systems efficient. Practice writing a few simple cron jobs on your system and monitor the output just like we did in this article.
If this is helpful to you, feel free to bookmark, comment, like and follow me for Day 14!
Let's Connect!
If you want to connect or share your journey, feel free to reach out on LinkedIn.
I am always happy to learn and build with others in the tech space.
#30DaysLinuxChallenge #Redhat#RHCSA #RHCE #CloudWhistler #Linux #Rhel #Ansible #Vim #CloudComputing #DevOps #LinuxAutomation #IaC #SysAdmin#CloudEngineer
Top comments (0)