Linux provides a robust built-in utility called cron for this purpose. Cron allows you to schedule commands or scripts to run automatically at specific times or intervals . Any task you schedule through cron is known as a cron job . This guide will walk you through the essentials of cron, demonstrate practical examples, and show you how to manage these automated tasks effectively.
Understanding Cron and Crontab
Cron is a time-based job scheduling daemon that runs in the background of Unix-like operating systems, including Linux . It continuously checks the /etc/crontab
file, the /etc/cron.*/
directories, and the individual user crontab files to see if any scheduled tasks need to be executed.
To interact with your personal cron jobs, you use the crontab
command. This command allows you to create, edit, list, and remove your scheduled tasks.
crontab -e
: Edits your personal crontab file.crontab -l
: Lists the contents of your personal crontab file .crontab -r
: Removes your personal crontab file.
Crontab Syntax
The syntax for a cron job is crucial to understand. Each line in a crontab file represents a job and follows this structure:
MIN HOUR DOM MON DOW CMD
- MIN: Minute (0-59)
- HOUR: Hour (0-23, 0 = midnight)
- DOM: Day of Month (1-31)
- MON: Month (1-12) or (Jan, Feb, …)
- DOW: Day of Week (0-7, where 0 and 7 represent Sunday) or (Sun, Mon, …)
- CMD: The command or script to execute.
Special characters like *
, /
, ,
, and -
can be used within these fields to define more complex schedules.
*
: Matches any value (e.g., every minute, every hour)./
: Defines steps (e.g.,*/5
in the MIN field means every 5 minutes).,
: Separates a list of values (e.g.,1,15
in DOM means the 1st and 15th).-
: Defines a range (e.g.,1-5
in DOW means Monday through Friday).
Practical Examples with Code and Output
Let’s dive into some practical examples to see how this works.
1. Creating Your First Cron Job
First, let’s create a simple script that logs the date and time to a file. Save this as ~/my_script.sh
:
#!/bin/bash
echo "Hello! The current date and time is $(date)" >> /home/$(whoami)/cron_log.txt
Make the script executable:
chmod +x ~/my_script.sh
Now, edit your crontab:
crontab -e
Add the following line to run the script every minute:
* * * * * /home/your_username/my_script.sh
(Replace your_username
with your actual username)
Output: After a minute, check the log file:
cat ~/cron_log.txt
Expected output (will grow every minute):
Hello! The current date and time is Sat Jul 26 10:00:01 UTC 2025
Hello! The current date and time is Sat Jul 26 10:01:01 UTC 2025
...
2. Scheduling a Daily Backup
Schedule a backup of the /home
directory to run every day at 2 AM:
crontab -e
Add the line:
0 2 * * * /bin/tar -czf /backups/home_backup_$(date +\%Y\%m\%d).tar.gz /home
(Ensure the /backups
directory exists and has appropriate permissions)
Explanation: 0 2 * * *
means run at minute 0 of hour 2, every day of the month, every month, every day of the week.
3. Running a Task Weekly
Run a system update every Sunday at midnight:
crontab -e
Add the line (for systems using apt
):
0 0 * * 0 /usr/bin/apt update && /usr/bin/apt upgrade -y
Explanation: 0 0 * * 0
means run at minute 0 of hour 0 (midnight) on day of the week 0 (Sunday).
4. Scheduling a Job Every 10 Minutes
Run a monitoring script every 10 minutes :
crontab -e
Add the line:
*/10 * * * * /path/to/monitor_script.sh
Explanation: */10
in the MIN field means every 10 minutes.
5. Scheduling a Job on Specific Days
Print “hello” at 22:30 (10:30 PM) every weekday (Monday to Friday) :
crontab -e
Add the line:
30 22 * * 1-5 echo "hello"
Or using day names:
30 22 * * Mon-Fri echo "hello"
6. Listing Your Cron Jobs
To see all your currently scheduled jobs :
crontab -l
Example Output:
* * * * * /home/your_username/my_script.sh
0 2 * * * /bin/tar -czf /backups/home_backup_$(date +\%Y\%m\%d).tar.gz /home
0 0 * * 0 /usr/bin/apt update && /usr/bin/apt upgrade -y
*/10 * * * * /path/to/monitor_script.sh
30 22 * * 1-5 echo "hello"
Managing Cron
- System-wide Cron: System-wide cron jobs are typically managed in
/etc/crontab
or the/etc/cron.d/
directory. The format is slightly different, requiring a username before the command:# Example entry in /etc/crontab 0 3 * * * root /usr/local/bin/system_cleanup.sh
- Anacron: For systems that are not always on (like laptops),
anacron
can be used to ensure jobs run even if the system was off during the scheduled time. - Logs: Cron job execution is usually logged. Check
/var/log/syslog
or/var/log/cron
(location varies by distribution) for logs:grep CRON /var/log/syslog
Conclusion
By mastering cron jobs, you gain a powerful tool for automating routine tasks on your Linux systems, leading to more efficient and reliable operations. Whether it’s daily backups, weekly updates, or custom monitoring scripts, cron provides the flexibility to schedule almost anything.