Hello! Let's dive into the next essential skill for a Linux administrator.
In our previous lesson, we gave our server a stable network identity by configuring a static IP address. Reliable servers are the foundation of any data center, but they also require regular, automated maintenance to stay healthy and secure. Manually running backups at 3 AM or checking log files every hour simply isn't feasible. This is where automation comes in.
Today, we will tackle the learning outcome: Schedule recurring administrative tasks using cron.
cron is the classic Linux task scheduler. It's a daemon (a background process) that wakes up every minute, checks a set of configuration files, and runs any tasks that are scheduled for that specific time. For a system administrator, mastering cron is non-negotiable. It's how you automate everything from daily backups and weekly package updates to monthly log rotation and system health checks.
In this lesson, we'll take a practice-first approach. We'll start with the basic commands, learn the scheduling syntax, and then put it to use with practical examples you'd encounter in a data center role.
1. Understanding the crontab
Every user on a Linux system can have their own list of scheduled tasks. This list is stored in a file called a crontab (cron table). You'll interact with your crontab using the crontab command.
Let's watch a short video that introduces the concept and the fundamental commands.
Linux Crash Course - Scheduling Tasks with Cron
The video 'Linux Crash Course - Scheduling Tasks with Cron' by Learn Linux TV provides a great starting point. It explains what cron jobs are and demonstrates the most common commands you'll use to manage them.
Watch from 00:59 to 02:38. Pay attention to the three main commands introduced: crontab -l (list): Shows your current scheduled jobs. crontab -e (edit): Opens your crontab file in a text editor so you can add, change, or remove jobs. crontab -r (remove): Deletes your entire crontab. Use this with caution!
As you saw, running crontab -e for the first time might prompt you to choose a text editor. I recommend selecting nano as it's the most straightforward for beginners.
2. The cron Scheduling Syntax
The most challenging part of using cron is learning its scheduling syntax. Each scheduled task is defined on a single line with six fields: five for the time and date, and one for the command to be executed.

The five time fields are:
- Minute (0-59)
- Hour (0-23, in 24-hour format)
- Day of the month (1-31)
- Month (1-12 or JAN-DEC)
- Day of the week (0-7, where both 0 and 7 are Sunday, or SUN-SAT)
An asterisk (*) in a field acts as a wildcard, meaning "every". For example, * * * * * means "every minute of every hour of every day..."
Let's watch a detailed explanation of how to use these fields to build a schedule.
Linux Crash Course - Scheduling Tasks with Cron
Let's continue with the Learn Linux TV video as it provides an excellent walkthrough of the scheduling syntax.
Watch from 02:38 to 07:46. The video covers: How to use numbers and asterisks to define specific schedules. How the fields work together to create precise timings (e.g., 'at 9:05 AM on the 15th of August'). Special shortcut strings like @hourly, @daily, and @reboot that simplify common schedules.
Pro Tip for Interview Prep: Remembering this syntax can be tricky. In a professional setting, many administrators use a helper tool to generate the correct syntax. A great one to know is crontab.guru. You can type in a schedule in plain English, and it gives you the cron expression, or vice-versa. Mentioning a tool like this shows practical awareness.
3. Your First Cron Job: A Hands-On Exercise
Theory is one thing, but practice is where the learning sticks. Let's create a simple cron job that runs every minute and writes the current date and time to a log file in your home directory. This gives you immediate feedback that cron is working.
-
Open your crontab for editing:
crontab -e -
Add the following line at the bottom of the file. This schedule
* * * * *means "run every minute".* * * * * echo "Job ran at $(date)" >> ~/cron_test.logecho "...": A command that prints a string.$(date)is a sub-command that gets replaced by the output of thedatecommand.>>: This is an output redirector. It appends the output of theechocommand to the specified file (cron_test.login your home directory,~).
-
Save the file and exit the editor. (In
nano, that'sCtrl+O,Enter, thenCtrl+X). -
Verify that your cron job was saved:
crontab -lYou should see the line you just added.
-
Now, wait for a minute or two. Then, check the contents of your log file:
cat ~/cron_test.logYou should see an entry for each minute that has passed since you created the job. If you wait another minute and run the
catcommand again, a new line will have appeared.
Congratulations, you've successfully scheduled your first automated task!
4. Cron Jobs for System Administration
While logging the date is a great test, real-world cron jobs perform critical administrative tasks. When doing so, you must consider which user should run the command.
- System-wide tasks (like updating packages, rebooting the system, or managing system services) should be run by the
rootuser. - Application-specific tasks (like clearing a web application's cache) should be run by the user that owns the application (e.g.,
www-datafor Apache).
You can edit the crontab for any user by using the -u flag with sudo.
# Edit the crontab for the root user
sudo crontab -u root -e
# List the crontab for the www-data user
sudo crontab -u www-data -l
The Two Most Common cron Pitfalls
When you run a command from your interactive shell, the system knows where to find commands like echo or apt because it has a PATH environment variable. Cron jobs run in a very minimal environment and often do not have the same PATH. This leads to the two most common reasons cron jobs fail.
To see this in action and learn how to avoid these critical mistakes, watch this last segment.
Linux Crash Course - Scheduling Tasks with Cron
The Learn Linux TV video explains this perfectly, demonstrating how to run jobs as other users and highlighting the importance of using full paths to commands.
Watch from 08:05 to 16:57. Focus on these two key takeaways: Running jobs as another user: Note how sudo crontab -u root -e is used to schedule a task as the root user. Using full paths: Pay close attention to the explanation around 15:40. You should always use the full path to a command or script in a cron job (e.g., /usr/bin/apt instead of just apt). This avoids any ambiguity. The command which <command_name> (e.g., which apt) can help you find the full path.
The second pitfall, related to the first, is script permissions. If you schedule a custom script (e.g., * * * * * /path/to/my_backup_script.sh), you must ensure that script has execute permissions (chmod +x /path/to/my_backup_script.sh).
5. Practical Examples and System-Wide Cron
So, what are some real-world administrative tasks you can automate? Here are a few examples. Notice the use of full paths.
-
Update package lists and upgrade packages every day at 3 AM (as root):
0 3 * * * /usr/bin/apt update && /usr/bin/apt upgrade -y -
Run a backup script every Sunday at 11 PM:
0 23 * * Sun /usr/local/bin/backup_script.sh -
Restart the Apache web server every morning at 4 AM for stability:
0 4 * * * /bin/systemctl restart apache2
For even more ideas, this article is a great reference to bookmark.
40 Practical Examples of Crontab in Linux
The article '40 Practical Examples of Crontab in Linux' from TecAdmin is an excellent resource full of real-world use cases. You don't need to read it all now, but glancing through it will give you a sense of the wide range of tasks that cron can automate.
Skim through the list of examples. Pay attention to examples like #2 (Backup), #10 (Delete Temp Files), #13 (Update Packages), and #25 (Restart Service). These are all common tasks in a data center environment.
Finally, besides user crontabs, there's another way to schedule system-wide tasks. Linux systems have special directories in /etc:
/etc/cron.hourly/etc/cron.daily/etc/cron.weekly/etc/cron.monthly
Any executable script you place in one of these directories will be run by root at the corresponding interval. This is often used by system packages to schedule their own maintenance tasks. It's a simpler alternative if you just need something to run daily or weekly and don't require a specific time.
Conclusion
You have now learned how to automate tasks on a Linux server, a core responsibility of any system administrator. You can confidently explain not just how to schedule a job, but why certain practices are essential for reliability.
Key Takeaways:
cronis the standard Linux tool for automating recurring tasks.- You manage jobs with
crontab -e(edit),crontab -l(list), andcrontab -r(remove). - A cron job consists of five time fields followed by the command. Use tools like
crontab.guruto help. - Always use full paths to commands and scripts in your crontab to avoid
PATHissues. - Run tasks as the appropriate user (
rootfor system tasks, service users for applications). - For simple hourly, daily, or weekly jobs, you can place a script in the
/etc/cron.*directories.
In our next lesson, we'll cover how to monitor disk usage with df and du. This ties in perfectly with what we've learned today, as a very common cron job is a script that uses df to check for low disk space and sends an email alert to the administrator.