Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
10 Cron Scheduling Task Examples in Linux
Linux is an operating system that is widely used in servers, supercomputers, and mobile devices. One of the powerful features of Linux is the ability to schedule tasks using a tool called Cron. Cron is a time-based job scheduler in Linux that allows users to run scripts or commands at specified intervals. In this article, we will discuss 10 practical Cron scheduling task examples in Linux.
What is Cron?
Cron is a time-based job scheduler in Linux that allows users to automate tasks at specified intervals. It can run scripts or commands at a specific time or a set of times. Cron is essential for system administrators because it automates tasks such as backups, system updates, and log rotation.
Cron uses a configuration file called crontab, which is located in the /etc/ directory. The crontab file contains a list of jobs that Cron will execute. Each job consists of a line with six fields separated by spaces. The fields represent the minute, hour, day of the month, month, day of the week, and command to be executed.
Crontab Field Format
Essential Cron Commands
Before diving into examples, here are the basic commands to manage crontab
# Edit crontab for current user crontab -e # List crontab entries crontab -l # Remove all crontab entries crontab -r # Edit crontab for another user (root only) crontab -u username -e
10 Practical Cron Scheduling Examples
1. Run a Script Every Minute
* * * * * /path/to/script.sh
This runs the script every minute of every day. Useful for monitoring tasks that need constant checking.
2. Run a Script Every Hour
0 * * * * /path/to/script.sh
Executes the script at the start of every hour (e.g., 1:00, 2:00, 3:00). Good for hourly log rotation or system checks.
3. Daily Backup at Midnight
0 0 * * * /path/to/backup.sh
Runs a backup script every day at midnight. Perfect for daily database backups or file synchronization.
4. Weekly System Update (Sunday Midnight)
0 0 * * 0 /usr/bin/apt update && /usr/bin/apt upgrade -y
Performs system updates every Sunday at midnight. Helps keep servers updated automatically.
5. Workday Reports (Monday to Friday at 9 AM)
0 9 * * 1-5 /path/to/generate_report.sh
Generates daily reports at 9 AM from Monday to Friday. Ideal for business reporting automation.
6. Bi-weekly Tasks (1st and 15th at Midnight)
0 0 1,15 * * /path/to/maintenance.sh
Runs maintenance tasks on the 1st and 15th of every month. Suitable for payroll processing or invoice generation.
7. Annual Holiday Script (Christmas at Noon)
0 12 25 12 * /path/to/holiday_greeting.sh
Executes a holiday script at 12:00 PM on December 25th every year. Can be used for sending automated holiday messages.
8. Frequent Monitoring (Every 10 Minutes)
*/10 * * * * /path/to/monitor.sh
Monitors system resources or services every 10 minutes. Essential for high-availability systems.
9. Business Hours Cleanup (Every 30 Minutes, 9 AM to 5 PM)
*/30 9-17 * * * /path/to/cleanup.sh
Cleans temporary files every 30 minutes during business hours. Prevents disk space issues during peak usage.
10. System Startup Tasks
@reboot /path/to/startup.sh
Runs the script every time the system boots. Perfect for starting custom services or mounting network drives.
Advanced Patterns
| Pattern | Description | Example |
|---|---|---|
| */5 0-8,18-23 * * * | Every 5 minutes outside business hours | Night monitoring |
| 0 2 * * 6,0 | 2 AM on weekends | Weekend maintenance |
| 15 14 1 * * | 2:15 PM on the 1st of every month | Monthly reports |
| 0 */4 * * * | Every 4 hours | Database synchronization |
Best Practices
Use absolute paths in scripts and commands to avoid PATH issues.
Redirect output to log files to track execution:
>> /var/log/cron.log 2>&1Test scripts manually before adding them to crontab.
Set environment variables at the top of crontab if needed.
Conclusion
Cron is a powerful automation tool that enables system administrators to schedule recurring tasks efficiently. From simple daily backups to complex business hour monitoring, these 10 examples demonstrate the versatility of cron scheduling. Understanding crontab syntax and following best practices ensures reliable task automation in Linux environments.
