Solaris uses the cron daemon (cron) as its main job scheduler. It allows administrators and users to automate tasks such as backups, log cleanup, monitoring scripts, database jobs, and system maintenance.
The cron service runs continuously in the background. It wakes up every minute, reads its configuration files (crontabs), and checks whether any job needs to be executed at that time.
1. The cron Daemon (crond)
- The cron daemon starts automatically during system boot.
- It runs as a background service.
- Every minute, it checks all crontab files.
- If the current time matches a crontab entry, it executes the command.
- Output of cron jobs is mailed to the user (if MAILTO is configured).
You can verify if cron is running:
# svcs cron
Restart cron service (Solaris SMF):
# svcadm restart cron
2. Cron Configuration Files
Solaris supports both system-wide and user-specific crontabs.
A. System-wide cron files
| File/Directory | Purpose |
|---|---|
| /etc/crontab | System-level scheduled jobs |
| /etc/cron.d | Additional cron files (Solaris 11) |
| /etc/cron.hourly | Hourly jobs |
| /etc/cron.daily | Daily jobs |
| /etc/cron.weekly | Weekly jobs |
| /etc/cron.monthly | Monthly jobs |
These directories contain executable scripts that are automatically run at predefined times.
B. User-specific cron files
User crontabs are stored in:
/var/spool/cron/crontabs/<username>
Each user can manage their own scheduled jobs without affecting others.
3. Cron Syntax and Time Fields
A standard crontab entry contains five time fields followed by the command.
* * * * * /path/to/command
│ │ │ │ │
│ │ │ │ └─ Day of week (0–7, Sunday=0 or 7)
│ │ │ └── Month (1–12)
│ │ └── Day of month (1–31)
│ └── Hour (0–23)
└── Minute (0–59)
Special Characters Used in Cron
| Symbol | Meaning |
|---|---|
| * | Every value |
| , | List (1,15,30) |
| - | Range (1-5) |
| / | Step values (*/5 every 5 minutes) |
Examples
Run every 5 minutes:
*/5 * * * * /scripts/check.sh
Run every day at midnight:
0 0 * * * /scripts/daily.sh
Run every Monday to Friday at 8 AM:
0 8 * * 1-5 /scripts/workday.sh
Run on 1st and 15th of every month:
0 3 1,15 * * /scripts/payroll.sh
4. Managing User Cron Jobs
List existing cron jobs
# crontab -l
Edit cron jobs
# crontab -e
This opens the default editor (vi).
Remove all cron jobs
# crontab -r
5. System Crontab Example
Example of /etc/crontab:
SHELL=/bin/sh
PATH=/usr/bin:/usr/sbin
MAILTO=root
0 1 * * * root /usr/lib/sa/sa1
Explanation:
- SHELL defines which shell to use.
- PATH defines executable search path.
- MAILTO sends job output to root.
- This example runs sa1 daily at 1:00 AM.
In system crontab, there is an extra field for user (root).
6. Environment Variables in Cron
Cron runs with a limited environment. If scripts depend on environment variables, define them explicitly.
Example:
- SHELL=/bin/bash
- PATH=/usr/local/bin:/usr/bin:/usr/sbin
- HOME=/export/home/user
- MAILTO=admin@example.com
Always use full paths in cron jobs to avoid command not found errors.
7. The at Command (One-Time Job Scheduling)
The at command is used to schedule a job that runs only once.
A. Schedule a Job
# echo "/usr/local/bin/backup.sh" | at 23:30
Schedule for a specific date:
# at 10:00 AM 10/25/2025
You will be placed in interactive mode:
Type commands
Press Ctrl+D to save
B. Manage at Jobs
List scheduled jobs:
# atq
Remove a job:
# atrm <job_number>
8. The batch Command
The batch command schedules jobs to run when system load is low.
Example:
# echo "/usr/local/bin/long_task.sh" | batch
The system checks load average.
If load is below threshold (usually 1.0), the job runs.
Good for CPU-intensive or large processing tasks.
9. Cron Security and Access Control
Solaris uses access control files:
/etc/cron.d/cron.allow
/etc/cron.d/cron.deny
Rules:
- If cron.allow exists → only listed users can use cron.
- If only cron.deny exists → users listed are denied.
- If neither exists → all users can use cron (except restricted accounts).
Same applies for at.allow and at.deny.
10. Cron Logging and Troubleshooting
Cron logs are typically found in:
/var/cron/log
To check execution history:
# tail -f /var/cron/log
Common issues:
- Script not executable (chmod +x script.sh)
- Wrong path
- Missing environment variables
- Permission issues
- Incorrect time format
11. Cron in Solaris Zones
In Solaris Zones:
- Each zone runs its own cron service.
- Jobs scheduled inside a non-global zone affect only that zone.
- Global zone cron does not control non-global zones.
- Resource limits in zones can impact cron job performance.
12. Best Practices for Scheduling Jobs
Always use full absolute paths.
Redirect output to log files:
0 2 * * * /scripts/backup.sh >> /var/log/backup.log 2>&1
Test scripts manually before scheduling.
Avoid overlapping jobs.
Monitor log files regularly.
Use batch for heavy jobs.
Document all production cron entries.
No comments:
Post a Comment