Unix - Cron and Job Scheduling in UNIX
Introduction
In UNIX systems, many administrative and maintenance tasks need to be performed regularly without requiring manual intervention. Examples include creating backups, cleaning temporary files, monitoring system performance, generating reports, and sending notifications. To automate such repetitive tasks, UNIX provides job scheduling utilities, the most common being cron and at.
Cron is a time-based job scheduler that executes commands or scripts automatically at specified intervals. It is one of the most important system administration tools because it helps automate routine operations and ensures tasks are completed consistently and on time.
What is Cron?
Cron is a background service, also known as a daemon, that runs continuously and checks for scheduled jobs. The daemon is commonly called crond or cron daemon.
When a scheduled time matches a configured task, the cron daemon automatically executes the corresponding command or script.
Cron is widely used for:
-
System backups
-
Log rotation
-
Database maintenance
-
File synchronization
-
Security scans
-
Email notifications
-
Automated report generation
Understanding the Cron Daemon
The cron daemon starts automatically when the UNIX system boots.
Its responsibilities include:
-
Reading scheduling information from cron tables.
-
Checking the current time every minute.
-
Executing commands when scheduled times match.
-
Logging execution results.
The daemon runs in the background without requiring user interaction.
What is a Crontab?
A crontab (cron table) is a configuration file that contains scheduling instructions.
Each user can have their own crontab file containing tasks they want executed automatically.
The crontab file consists of:
-
Scheduling information
-
Commands to execute
Each line in the file represents one scheduled job.
Crontab Command Syntax
To edit a user's cron schedule:
crontab -e
To display current scheduled jobs:
crontab -l
To remove all scheduled jobs:
crontab -r
To replace a crontab with another file:
crontab filename
Structure of a Cron Entry
A cron job consists of six fields:
* * * * * command
The fields represent:
Minute Hour Day Month Weekday Command
Detailed breakdown:
| Field | Range |
|---|---|
| Minute | 0–59 |
| Hour | 0–23 |
| Day of Month | 1–31 |
| Month | 1–12 |
| Day of Week | 0–7 |
| Command | Task to execute |
In many UNIX systems:
0 = Sunday
7 = Sunday
Example of a Cron Job
30 2 * * * /home/user/backup.sh
Meaning:
-
Minute = 30
-
Hour = 2
-
Every day
-
Every month
-
Every weekday
The script executes daily at 2:30 AM.
Special Characters Used in Cron
Asterisk (*)
Represents all possible values.
Example:
* * * * * command
Runs every minute.
Comma (,)
Specifies multiple values.
Example:
0 9,18 * * * command
Runs at 9 AM and 6 PM.
Hyphen (-)
Specifies a range.
Example:
0 9-17 * * * command
Runs every hour from 9 AM through 5 PM.
Slash (/)
Specifies intervals.
Example:
*/10 * * * * command
Runs every 10 minutes.
Common Scheduling Examples
Every Day at Midnight
0 0 * * * script.sh
Every Sunday
0 5 * * 0 script.sh
Runs at 5:00 AM every Sunday.
Every 15 Minutes
*/15 * * * * script.sh
Every Month on the First Day
0 0 1 * * script.sh
Every Weekday
0 8 * * 1-5 script.sh
Runs Monday through Friday at 8:00 AM.
System-Wide Cron Jobs
Apart from user-specific crontabs, UNIX systems often maintain system-wide cron configurations.
Common locations include:
/etc/crontab
and
/etc/cron.d/
System administrators use these files to schedule tasks affecting the entire system.
Examples:
-
Security updates
-
System cleanup
-
Log maintenance
-
Service monitoring
Environment Variables in Cron
Cron jobs run in a limited environment.
Important variables include:
PATH
HOME
SHELL
MAILTO
Example:
PATH=/usr/bin:/bin
[email protected]
These settings help control command execution and notification behavior.
Redirecting Output
Cron captures output generated by commands.
To store output in a file:
0 1 * * * script.sh > output.log
To capture errors:
0 1 * * * script.sh 2> error.log
To capture both:
0 1 * * * script.sh > output.log 2>&1
This is useful for troubleshooting scheduled jobs.
Using Scripts with Cron
Instead of writing lengthy commands in the crontab, administrators usually schedule scripts.
Example shell script:
#!/bin/sh
echo "Backup Started"
tar -czf backup.tar.gz /data
echo "Backup Completed"
Cron entry:
0 2 * * * /home/admin/backup.sh
Benefits:
-
Easier maintenance
-
Better readability
-
Reusable automation
The at Command
While cron handles recurring jobs, the at command schedules a task for a single execution.
Example:
at 10:00 PM
Then enter:
backup.sh
Press:
Ctrl+D
The task executes once at 10 PM.
Managing at Jobs
View scheduled jobs:
atq
Remove a job:
atrm job_number
Example:
atrm 3
Deletes job number 3.
Difference Between Cron and At
| Feature | Cron | At |
|---|---|---|
| Purpose | Repeated tasks | One-time tasks |
| Scheduling | Recurring | Single execution |
| Configuration | Crontab | At command |
| Usage | Routine automation | Future task execution |
| Examples | Daily backups | Run report tomorrow |
Security Considerations
UNIX systems often control cron access through:
/etc/cron.allow
and
/etc/cron.deny
These files determine which users can schedule cron jobs.
Administrators use them to prevent unauthorized task scheduling.
Best Practices for Cron Jobs
Use Absolute Paths
Instead of:
backup.sh
Use:
/home/user/scripts/backup.sh
Log Execution Results
Always store output:
0 2 * * * backup.sh >> backup.log 2>&1
Test Scripts Manually
Before scheduling:
./backup.sh
Verify that it runs correctly.
Avoid Resource-Heavy Jobs During Peak Hours
Schedule large backups or reports during off-peak times.
Add Comments
Document cron jobs clearly.
Example:
# Daily database backup
0 1 * * * db_backup.sh
Advantages of Cron Scheduling
-
Automates repetitive tasks.
-
Reduces manual effort.
-
Improves system reliability.
-
Ensures consistent execution.
-
Supports unattended operations.
-
Enhances productivity.
-
Simplifies system administration.
Limitations of Cron
-
Limited scheduling flexibility compared to modern schedulers.
-
Difficult to manage very complex workflows.
-
Jobs may fail silently if logging is not configured.
-
Requires careful time configuration.
-
Dependency handling is limited.
Conclusion
Cron is one of the most powerful and widely used job scheduling utilities in UNIX. It enables administrators and users to automate recurring tasks efficiently, improving productivity and system reliability. Through crontab files, users can define schedules ranging from every minute to specific dates and times. Combined with the at command for one-time execution, UNIX provides a comprehensive framework for task automation, making cron an essential tool for system administration, maintenance, monitoring, and operational efficiency.