Part 6: Cron and Crontab in Linux
In this section, we will dive into the concepts of cron
and crontab
, two essential tools for scheduling tasks in Linux. We’ll explain their purpose, how to use them, and provide practical examples for automation.
What is cron
?
cron
is a time-based job scheduler in Unix-like operating systems. It allows users to schedule tasks (commands or scripts) to run automatically at specified intervals, such as daily, weekly, or monthly.
What is crontab
?
crontab
(short for "cron table") is a configuration file used by the cron
daemon to define the schedule for running tasks. Each user on the system can have their own crontab
file.
How to Use crontab
The crontab
command is used to manage the crontab
file. Commonly used options include:
crontab -e
: Edit the current user’scrontab
file.crontab -l
: List all active crontab entries for the current user.crontab -r
: Remove the current user’scrontab
file.
Crontab Syntax
A crontab entry follows this format:
minute hour day month weekday command
Where:
minute: 0-59
hour: 0-23
day: 1-31
month: 1-12
weekday: 0-6 (Sunday = 0 or 7)
command: The script or command to execute
Special Syntax
*
: Matches every value (e.g., every minute, hour, etc.),
: Separates multiple values (e.g.,1,5,10
means at 1, 5, and 10)-
: Specifies a range (e.g.,1-5
means from 1 to 5)/
: Specifies step values (e.g.,*/5
means every 5 units)
Examples of Crontab Usage
Example 1: Run a Script Every Day at Midnight
0 0 * * * /path/to/script.sh
This runs script.sh
at 12:00 AM every day.
Example 2: Backup Files Every Sunday at 2:30 AM
30 2 * * 0 /path/to/backup.sh
This runs backup.sh
at 2:30 AM on Sundays.
Example 3: Clear Temporary Files Every Hour
0 * * * * rm -rf /tmp/*
This clears the /tmp
directory at the beginning of every hour.
Example 4: Run a Command on Specific Days
0 6 1,15 * * /path/to/report.sh
This runs report.sh
at 6:00 AM on the 1st and 15th of each month.
How to Show Active Crontabs
To view all active crontab entries for the current user, use:
crontab -l
Output:
# Example crontab entries
0 0 * * * /path/to/script.sh
30 2 * * 0 /path/to/backup.sh
How to Edit Crontabs
To edit the current user’s crontab file, use:
crontab -e
This opens the crontab file in the default text editor. After making changes, save and exit to update the scheduled tasks.
Switching the Default Editor
If you prefer a specific text editor, you can set it before editing crontabs:
export EDITOR=vim
crontab -e
Removing Crontab Entries
To remove all crontab entries for the current user, use:
crontab -r
Use with caution, as this deletes all scheduled tasks.
Practical Examples of Automation with Cron
Example: Monitoring Disk Space
Add an entry to check disk space and email a warning if usage exceeds 90%:
*/10 * * * * /path/to/disk_space_monitor.sh
This runs disk_space_
monitor.sh
every 10 minutes.
Example: Syncing Files with a Remote Server
Schedule a task to sync files with an external server every day:
0 3 * * * rsync -avz /local/dir/ user@remote:/backup/dir/
Example: Restarting a Service Automatically
Restart a web server every day at 4:00 AM:
0 4 * * * systemctl restart apache2
Creating a Shell Script to Display Active Crontabs
Script: Displaying Active Crontabs with Descriptions
#!/bin/bash
# Display header
echo "Active crontabs for $(whoami):"
# List crontabs
crontab -l | awk '{print NR ".", $0}'
Summary
Cron: Schedules tasks.
Crontab: Manages the schedule.
Syntax: Minute, hour, day, month, weekday, and command.
Commands:
crontab -e
,crontab -l
,crontab -r
.Use cases include backups, monitoring, and automation.
Cron and crontab are essential for automating repetitive tasks in Linux. Try creating your own schedules to gain practical experience!