1. What is Cron?
Cron is a time-based job scheduler for Linux and Unix-based operating systems. It is primarily used by system administrators and developers to automate tasks that need to be executed regularly. Cron comes pre-installed on Ubuntu and is widely used for server management, backups, scheduled script execution, and various other automation tasks.
How Cron Works
Cron operates using a file called “crontab,” where commands are specified to run at designated times or intervals. The crontab file consists of five fields, each allowing precise scheduling of tasks based on the specified values.
- Minute (0–59)
- Hour (0–23)
- Day of the month (1–31)
- Month (1–12)
- Day of the week (0–7, where 0 and 7 represent Sunday)
For example, if you want to schedule a backup task to run every day at 5 AM, you would add the following line to your crontab file:
0 5 * * * tar -zcf /var/backups/home.tgz /home/
This demonstrates how Cron can be used to automate recurring jobs efficiently.
Who Should Use Cron?
Cron is essential for system administrators, developers looking to streamline daily tasks, and anyone who needs to run scheduled scripts on a server environment.

2. Setting Up Cron Jobs
Editing the Crontab File
To set up a Cron job, you need to access and edit the “crontab” file. On Ubuntu, you can open your user-specific crontab file using the following command:
crontab -e
Basic Syntax of a Cron Job
A Cron job entry consists of time fields followed by the command to execute. The general format is:
Minute Hour Day Month Weekday Command
For example, the following Cron job creates a backup of the /home/
directory every day at 5 AM:
0 5 * * * tar -zcf /var/backups/home.tgz /home/
Saving and Verifying Crontab Changes
Once you’ve added a job to the crontab file, save and close the editor to apply the changes. To verify your current Cron jobs, use the following command:
crontab -l
3. Advanced Cron Job Scheduling
Executing Jobs at Custom Intervals
To execute a job at specific intervals, such as every minute or every five minutes, use the following syntax:
- Every minute:
* * * * * /path/to/script.sh
- Every five minutes:
*/5 * * * * /path/to/script.sh
Running Jobs on Specific Days and Times
If you need to run a task on specific days, use the weekday field. For example, to run a script every Monday at 2:15 AM, use:
15 2 * * 1 /path/to/script.sh
4. Error Handling and Troubleshooting
Common Issues with Cron Jobs
Cron Job Not Executing
If your Cron job isn’t running, check the following key points:
- Check Permissions: Ensure the script or command has execution permissions.
- Use Full Paths: Cron runs in a limited environment, so specify the full path to your commands and files.
/usr/bin/python3 /path/to/script.py
Checking Cron Logs
Cron logs its execution details in /var/log/syslog
. To check the logs for Cron-related errors, use:
grep CRON /var/log/syslog

5. Security Considerations
Controlling User Access
To restrict which users can schedule Cron jobs, you can use the /etc/cron.allow
and /etc/cron.deny
files. By listing users in /etc/cron.allow
, only those users will be permitted to create Cron jobs.
echo "user_name" >> /etc/cron.allow
Login Security and Cron Jobs
When running Cron jobs that require authentication, it’s important to avoid password-related errors. Using SSH key automation or a secure password manager can help prevent authentication failures.
6. Using Anacron: Automating Infrequent Tasks
What is Anacron?
Anacron is a job scheduler designed for systems that are not running continuously. Unlike Cron, which requires the system to be online at the scheduled time, Anacron ensures that missed jobs run when the system is next powered on. This makes it ideal for desktops and laptops.
7. Practical Use Cases for Cron Jobs
Automating Backups
Setting up a Cron job to automate backups on a regular schedule can help prevent data loss. Below is an example Cron job that creates a backup of the /home/
directory every day at 2 AM and includes the current date in the filename.
0 2 * * * tar -zcf /var/backups/home_backup_$(date +%Y-%m-%d).tgz /home/
8. Conclusion
By leveraging Cron and Anacron, you can efficiently automate recurring tasks and improve the reliability of your system operations. When used correctly, these tools reduce operational costs and ensure that essential maintenance tasks run without manual intervention. Implement them in your system to experience the benefits of automation.