USD ($)
$
United States Dollar
Euro Member Countries
India Rupee

Task Scheduling with Cron

Lesson 28/49 | Study Time: 20 Min

Cron is a powerful and widely used Linux utility for scheduling repetitive tasks to run automatically at specified times or intervals. It allows users and administrators to automate system maintenance, backups, monitoring scripts, and other routine operations without manual intervention. 

How Cron Works

Cron works by using the cron daemon (crond), a background process that runs continuously and checks defined schedules to determine when tasks should be executed.

Each user can maintain a personalized crontab file, which contains a list of scheduled commands or scripts to run automatically at specified times.

In addition to user-specific crontabs, Linux also supports a system-wide crontab, usually located at /etc/crontab, along with directories such as /etc/cron.d/, where administrators can configure and manage scheduled system-level tasks.

The Crontab File Syntax

Each line in a crontab defines a task with the following format:

text
* * * * * command_to_execute
- - - - -
| | | | |
| | | | +---- Day of the week (0-6 or Sun-Sat)
| | | +------ Month (1-12 or Jan-Dec)
| | +-------- Day of the Month (1-31)
| +---------- Hour (0-23)
+------------ Minute (0-59)


Use an asterisk (*) to denote “every” for a field. You can specify ranges (1-5), lists (1,2,5), or step values (*/10 means every 10 units).

Common Examples

Managing Your Crontab

The following list explains how users can manage their crontab using built-in Linux commands. These operations are essential for automation and system administration.


1. View crontab entries:

text
crontab -l


2. Edit crontab for current user:

text
crontab -e

This opens the crontab file in your default editor.


3. Remove current user crontab:

text
crontab -r

Example Cron Job

To run a backup script every day at 2:30 AM, add this line to your crontab:

text
30 2 * * * /home/user/backup.sh

Ensure the script has executable permissions (chmod +x backup.sh) and that you use absolute paths for commands within the script.

Special Strings for Scheduling

Special scheduling keywords in cron provide clarity and ease of use. The list below outlines these standard execution intervals.


1. @reboot: Run once at system startup.

2. @hourly: Run once every hour.

3. @daily or @midnight: Run once a day at midnight.

4. @weekly: Run once a week.

5. @monthly: Run once a month.

6. @yearly or @annually: Run once a year.


Example:

text
@reboot /home/user/startup_script.sh

Output Handling

By default, cron sends the output and errors to the user’s email.

  • Redirect output to a file for logging:
text
* * * * * /path/to/script.sh >> /path/to/logfile.log 2>&1

Troubleshooting Cron Jobs


1. Verify cron service is running:

text
sudo systemctl status crond


2. Check logs (e.g., /var/log/cron, /var/log/syslog) for cron activity.

3. Use absolute paths for all executable commands in cron jobs.

4. Use scripts with proper permissions and test them manually before scheduling.

Samuel Wilson

Samuel Wilson

Product Designer
Profile

Class Sessions

1- What is Linux and Operating System Concepts 2- Linux History and Evolution 3- Linux Distributions and Their Purposes 4- Open Source Software and Licensing 5- Graphical User Interface (GUI) and Desktop Environments 6- Terminal Access and Command-Line Fundamentals 7- Getting Help and Command Documentation 8- File System Hierarchy and Directory Structure 9- Navigating Directories and Listing Contents 10- Creating, Copying, and Moving Files and Directories 11- Deleting Files and Directories 12- Symbolic and Hard Links 13- Understanding File Permissions Model 14- Modifying Permissions and Ownership 15- User and Group Management 16- Sudo and Privilege Escalation 17- Text Searching and Pattern Matching 18- Text Processing and Stream Editing 19- Compressing and Archiving Files 20- Text Editing and File Creation 21- Package Management Systems Overview 22- Installing and Updating Software with APT 23- Installing and Updating Software with YUM/DNF 24- Managing Software from Non-Repository Sources 25- Understanding Processes and Process Management 26- Viewing Running Processes 27- Process Control and Termination 28- Task Scheduling with Cron 29- Networking Concepts and IP Addressing 30- Viewing and Configuring Network Interfaces 31- Basic Network Troubleshooting 32- Shell Script Basics 33- Variables and Data Types 34- Conditional Logic in Scripts 35- Loops and Iteration 36- Functions and Code Reuse 37- Input/Output and User Interaction 38- System Authentication and Access Control 39- File System Security 40- Software Updates and Patching 41- Basic Firewall Concepts 42- System Information and Monitoring 43- Service and Daemon Management 44- System Boot Process and Runlevels 45- System Backup and Disaster Recovery 46- Comprehensive File System Management 47- System Automation Workflows 48- Multi-Concept Troubleshooting Scenarios 49- Continued Learning Pathways