Skip to content

Linux: schedule and automate tasks

Haikel Fazzani Haikel Fazzani
2025-08-01

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 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

Special characters like *, /, ,, and - can be used within these fields to define more complex schedules.

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

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.


Linux cron crontab automation scheduling tasks scripts system administration sysadmin bash command line job scheduling

More Insights.