Skip to main content

Cron Jobs va Task Scheduling

Task scheduling - bu Linux tizimlarida avtomatik vazifa bajarish uchun muhim vosita. Cron eng keng qo'llaniladigan scheduling tool bo'lib, systemd timer'lar zamonaviy alternativ hisoblanadi.

Cron Asoslari

Cron Tizimi

# Cron daemon status
systemctl status cron # Ubuntu/Debian
systemctl status crond # CentOS/RHEL
ps aux | grep cron # Process check

# Cron service management
sudo systemctl start cron
sudo systemctl enable cron
sudo systemctl restart cron

Cron File'lar va Directory'lar

# User crontab files
/var/spool/cron/crontabs/username # Ubuntu/Debian
/var/spool/cron/username # CentOS/RHEL

# System crontab
/etc/crontab # System-wide crontab

# System cron directories
/etc/cron.d/ # Additional cron files
/etc/cron.hourly/ # Hourly scripts
/etc/cron.daily/ # Daily scripts
/etc/cron.weekly/ # Weekly scripts
/etc/cron.monthly/ # Monthly scripts

# Cron configuration
/etc/cron.allow # Allowed users
/etc/cron.deny # Denied users

Crontab Syntax

Crontab Format

# Crontab entry format:
# Minute Hour Day Month DayOfWeek Command
# 0-59 0-23 1-31 1-12 0-7 /path/to/command

# Field meanings:
# Minute (0-59)
# Hour (0-23)
# Day of Month (1-31)
# Month (1-12)
# Day of Week (0-7, 0 and 7 = Sunday)

# Examples:
30 2 * * * # Daily at 2:30 AM
0 */6 * * * # Every 6 hours
0 9 * * 1-5 # 9 AM on weekdays
0 0 1 * * # First day of every month
*/15 * * * * # Every 15 minutes

Special Characters

# Asterisk (*) - any value
* * * * * command # Every minute

# Comma (,) - list of values
0 9,17 * * * # 9 AM and 5 PM

# Hyphen (-) - range of values
0 9-17 * * * # Every hour from 9 AM to 5 PM

# Slash (/) - step values
*/10 * * * * # Every 10 minutes
0 */2 * * * # Every 2 hours

# Question mark (?) - either day of month or day of week
0 0 15 * ? # 15th of every month
0 0 ? * 1 # Every Monday

Special Time Strings

@reboot              # Run at startup
@yearly # @annually # Run once a year (0 0 1 1 *)
@monthly # Run once a month (0 0 1 * *)
@weekly # Run once a week (0 0 * * 0)
@daily # @midnight # Run once a day (0 0 * * *)
@hourly # Run once an hour (0 * * * *)

# Examples:
@reboot /path/to/startup-script.sh
@daily /usr/local/bin/backup.sh
@weekly /usr/local/bin/cleanup.sh

Crontab Management

User Crontab

# Edit user crontab
crontab -e # Edit current user's crontab
crontab -e -u username # Edit specific user's crontab

# List user crontab
crontab -l # List current user's crontab
crontab -l -u username # List specific user's crontab

# Remove user crontab
crontab -r # Remove current user's crontab
crontab -r -u username # Remove specific user's crontab

# Install crontab from file
crontab filename # Install crontab from file
crontab -u username filename # Install for specific user

System Crontab

# Edit system crontab
sudo vim /etc/crontab

# System crontab format (with user field):
# Minute Hour Day Month DayOfWeek User Command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

# Cron directories
ls -la /etc/cron.daily/ # Daily scripts
ls -la /etc/cron.hourly/ # Hourly scripts
ls -la /etc/cron.weekly/ # Weekly scripts
ls -la /etc/cron.monthly/ # Monthly scripts

Cron.d Directory

# Create cron job in /etc/cron.d/
sudo vim /etc/cron.d/myapp

# Format (with user field):
# Minute Hour Day Month DayOfWeek User Command
0 2 * * * myapp /opt/myapp/bin/backup.sh
30 */6 * * * root /usr/local/bin/system-check.sh

# File permissions
sudo chmod 644 /etc/cron.d/myapp
sudo chown root:root /etc/cron.d/myapp

Practical Cron Examples

Basic Maintenance Tasks

# System updates (be careful with automatic updates)
0 3 * * 0 apt update && apt list --upgradable >> /var/log/updates.log

# Log rotation
0 0 * * * /usr/sbin/logrotate /etc/logrotate.conf

# Disk cleanup
0 2 * * 0 find /tmp -type f -mtime +7 -delete
0 3 * * 0 find /var/log -name "*.log" -mtime +30 -delete

# Database backup
0 1 * * * mysqldump --all-databases | gzip > /backup/mysql_$(date +\%Y\%m\%d).sql.gz

# File synchronization
*/30 * * * * rsync -av /data/ user@backup-server:/backup/data/

Application-Specific Tasks

# Web server log analysis
0 1 * * * /usr/local/bin/analyze-logs.sh >> /var/log/log-analysis.log

# Clear cache
0 */4 * * * rm -rf /var/cache/myapp/*

# Application health check
*/5 * * * * /usr/local/bin/health-check.sh

# Data processing
0 2 * * * /opt/myapp/bin/process-daily-data.sh

# Report generation
0 9 * * 1 /usr/local/bin/weekly-report.sh | mail -s "Weekly Report" admin@company.com

Environment Variables in Cron

# Set environment variables in crontab
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=admin@company.com
HOME=/home/user

# Use environment variables
0 2 * * * $HOME/scripts/backup.sh

# Source profile in script
0 3 * * * /bin/bash -l -c '/home/user/scripts/app-maintenance.sh'

Advanced Cron Techniques

Cron with Logging

# Redirect output to log file
0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

# Separate stdout and stderr
0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>> /var/log/backup-error.log

# Log with timestamp
0 2 * * * echo "$(date): Starting backup" >> /var/log/backup.log; /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

# Using logger command
0 2 * * * /usr/local/bin/backup.sh 2>&1 | logger -t backup

Conditional Cron Jobs

# Run only if file exists
0 2 * * * [ -f /tmp/run-backup ] && /usr/local/bin/backup.sh

# Run only on specific server
0 2 * * * [ "$(hostname)" = "prod-server" ] && /usr/local/bin/backup.sh

# Run only if service is running
*/5 * * * * systemctl is-active nginx >/dev/null && /usr/local/bin/nginx-monitor.sh

# Run only during business hours
0 9-17 * * 1-5 /usr/local/bin/business-hours-task.sh

Lock Files (Prevent Overlapping)

#!/bin/bash
# backup-with-lock.sh - Prevent overlapping cron jobs

LOCKFILE="/var/lock/backup.lock"
LOGFILE="/var/log/backup.log"

# Function to log messages
log_message() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOGFILE"
}

# Check for lock file
if [ -f "$LOCKFILE" ]; then
log_message "Backup already running (lock file exists)"
exit 1
fi

# Create lock file
echo $$ > "$LOCKFILE"

# Cleanup function
cleanup() {
rm -f "$LOCKFILE"
log_message "Backup completed, lock file removed"
}

# Set trap for cleanup
trap cleanup EXIT

# Main backup logic
log_message "Starting backup process"
# Your backup commands here
sleep 300 # Simulate backup time
log_message "Backup finished successfully"

Complex Scheduling Examples

# Business hours only (9 AM - 5 PM, Monday-Friday)
0 9-17 * * 1-5 /usr/local/bin/business-task.sh

# Every 2 hours during work days
0 */2 * * 1-5 /usr/local/bin/frequent-task.sh

# Last day of every month
0 0 28-31 * * [ $(date -d tomorrow +\%d) -eq 1 ] && /usr/local/bin/month-end.sh

# First Monday of every month
0 9 1-7 * 1 /usr/local/bin/first-monday.sh

# Every 15 minutes between 9 AM and 5 PM
*/15 9-17 * * * /usr/local/bin/frequent-check.sh

# Weekend maintenance (Saturday 2 AM)
0 2 * * 6 /usr/local/bin/weekend-maintenance.sh

Cron Troubleshooting

Common Issues

# Check cron service status
systemctl status cron
systemctl status crond

# Check cron logs
tail -f /var/log/cron # CentOS/RHEL
tail -f /var/log/syslog | grep CRON # Ubuntu/Debian
journalctl -u cron -f # systemd systems

# Check user permissions
cat /etc/cron.allow # Allowed users
cat /etc/cron.deny # Denied users

# Verify crontab syntax
crontab -l | crontab -T # Test syntax (some systems)

Debugging Cron Jobs

#!/bin/bash
# debug-cron.sh - Debug cron job issues

# 1. Environment debugging
env > /tmp/cron-env.txt # Save environment
echo "PATH: $PATH" >> /tmp/cron-debug.log
echo "USER: $USER" >> /tmp/cron-debug.log
echo "HOME: $HOME" >> /tmp/cron-debug.log
echo "PWD: $PWD" >> /tmp/cron-debug.log

# 2. Command debugging
which command_name >> /tmp/cron-debug.log
ls -la /path/to/script >> /tmp/cron-debug.log

# 3. Test script manually
/bin/bash -l -c '/path/to/script' >> /tmp/cron-test.log 2>&1

# 4. Minimal test cron job
# */1 * * * * echo "Cron is working at $(date)" >> /tmp/cron-test.log

Monitoring Cron Jobs

#!/bin/bash
# cron-monitor.sh - Monitor cron job execution

LOG_FILE="/var/log/cron-monitor.log"
CRON_LOG="/var/log/cron"

# Function to log messages
log_message() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE"
}

# Check recent cron executions
check_cron_activity() {
log_message "Checking cron activity..."

# Check if cron daemon is running
if systemctl is-active cron >/dev/null 2>&1 || systemctl is-active crond >/dev/null 2>&1; then
log_message "Cron daemon is running"
else
log_message "ERROR: Cron daemon is not running"
return 1
fi

# Check recent cron job executions
recent_jobs=$(grep "$(date '+%b %d')" "$CRON_LOG" 2>/dev/null | wc -l)
log_message "Recent cron jobs executed: $recent_jobs"

# Check for failed cron jobs
failed_jobs=$(grep "$(date '+%b %d')" "$CRON_LOG" 2>/dev/null | grep -i "error\|fail" | wc -l)
if [ "$failed_jobs" -gt 0 ]; then
log_message "WARNING: $failed_jobs failed cron jobs detected"
grep "$(date '+%b %d')" "$CRON_LOG" | grep -i "error\|fail" | tail -5 >> "$LOG_FILE"
fi
}

# Check specific user cron jobs
check_user_crons() {
log_message "Checking user cron configurations..."

for user in $(cut -d: -f1 /etc/passwd); do
if crontab -l -u "$user" >/dev/null 2>&1; then
job_count=$(crontab -l -u "$user" | grep -v '^#' | grep -v '^$' | wc -l)
if [ "$job_count" -gt 0 ]; then
log_message "User $user has $job_count cron jobs"
fi
fi
done
}

# Main execution
check_cron_activity
check_user_crons
log_message "Cron monitoring completed"

systemd Timer (Modern Alternative)

Timer vs Cron

# Advantages of systemd timers:
# - Better logging with journalctl
# - Service dependencies
# - More precise timing
# - Better error handling
# - Integration with systemd

# Disadvantages:
# - More complex syntax
# - Less universal (systemd specific)
# - Requires two files (service + timer)

Creating systemd Timer

# 1. Create service file
sudo vim /etc/systemd/system/backup.service

[Unit]
Description=Daily Backup Service
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
User=backup
Group=backup
StandardOutput=journal
StandardError=journal

# 2. Create timer file
sudo vim /etc/systemd/system/backup.timer

[Unit]
Description=Run backup daily
Requires=backup.service

[Timer]
OnCalendar=daily
Persistent=true
RandomizedDelaySec=300

[Install]
WantedBy=timers.target

# 3. Enable and start timer
sudo systemctl daemon-reload
sudo systemctl enable backup.timer
sudo systemctl start backup.timer

Timer Scheduling Syntax

# OnCalendar examples:
hourly # Top of every hour
daily # Every day at midnight
weekly # Every Monday at midnight
monthly # First day of month at midnight
yearly # January 1st at midnight

# Specific times:
*-*-* 02:30:00 # Daily at 2:30 AM
Mon *-*-* 02:30:00 # Monday at 2:30 AM
*-*-01 02:30:00 # First day of month at 2:30 AM

# Complex scheduling:
Mon,Tue,Wed,Thu,Fri *-*-* 09:00:00 # Weekdays at 9 AM
*-*-* 09,12,15,18:00:00 # 9 AM, 12 PM, 3 PM, 6 PM

# Timer intervals:
OnBootSec=15min # 15 minutes after boot
OnUnitActiveSec=1h # 1 hour after last activation
OnActiveSec=30min # 30 minutes after timer activation

Timer Management

# List timers
systemctl list-timers # Active timers
systemctl list-timers --all # All timers

# Timer status
systemctl status backup.timer # Timer status
systemctl status backup.service # Service status

# Timer logs
journalctl -u backup.timer # Timer logs
journalctl -u backup.service # Service logs

# Manual trigger
sudo systemctl start backup.service # Run service manually

Automated Task Examples

System Maintenance Scripts

#!/bin/bash
# system-maintenance.sh - Comprehensive system maintenance

SCRIPT_NAME="System Maintenance"
LOG_FILE="/var/log/system-maintenance.log"
MAIL_TO="admin@company.com"

# Logging function
log_message() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}

# Start maintenance
log_message "$SCRIPT_NAME started"

# 1. Update package database
log_message "Updating package database..."
if command -v apt >/dev/null 2>&1; then
apt update >> "$LOG_FILE" 2>&1
elif command -v yum >/dev/null 2>&1; then
yum check-update >> "$LOG_FILE" 2>&1
fi

# 2. Clean package cache
log_message "Cleaning package cache..."
if command -v apt >/dev/null 2>&1; then
apt autoclean >> "$LOG_FILE" 2>&1
apt autoremove -y >> "$LOG_FILE" 2>&1
elif command -v yum >/dev/null 2>&1; then
yum clean all >> "$LOG_FILE" 2>&1
fi

# 3. Clean temporary files
log_message "Cleaning temporary files..."
find /tmp -type f -mtime +7 -delete 2>> "$LOG_FILE"
find /var/tmp -type f -mtime +30 -delete 2>> "$LOG_FILE"

# 4. Rotate logs
log_message "Rotating logs..."
logrotate /etc/logrotate.conf >> "$LOG_FILE" 2>&1

# 5. Check disk space
log_message "Checking disk space..."
df -h | awk '$5 > 80 {print "WARNING: " $0 " is over 80% full"}' >> "$LOG_FILE"

# 6. Check failed services
log_message "Checking failed services..."
failed_services=$(systemctl --failed --no-legend | wc -l)
if [ "$failed_services" -gt 0 ]; then
log_message "WARNING: $failed_services failed services detected"
systemctl --failed >> "$LOG_FILE"
fi

# 7. System summary
log_message "System summary:"
echo "Uptime: $(uptime)" >> "$LOG_FILE"
echo "Memory: $(free -h | grep Mem)" >> "$LOG_FILE"
echo "Load: $(cat /proc/loadavg)" >> "$LOG_FILE"

log_message "$SCRIPT_NAME completed"

# Send email report if mail is configured
if command -v mail >/dev/null 2>&1; then
tail -50 "$LOG_FILE" | mail -s "System Maintenance Report - $(hostname)" "$MAIL_TO"
fi

Database Backup Automation

#!/bin/bash
# database-backup.sh - Automated database backup

# Configuration
DB_HOST="localhost"
DB_USER="backup_user"
DB_PASS_FILE="/etc/mysql/backup.password"
BACKUP_DIR="/backup/mysql"
RETENTION_DAYS=30
LOG_FILE="/var/log/mysql-backup.log"

# Read password from file
if [ -f "$DB_PASS_FILE" ]; then
DB_PASS=$(cat "$DB_PASS_FILE")
else
echo "Password file not found: $DB_PASS_FILE" >&2
exit 1
fi

# Logging function
log_message() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}

# Create backup directory
mkdir -p "$BACKUP_DIR"

# Start backup
log_message "Starting MySQL backup..."

# Get list of databases
databases=$(mysql -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASS" -e "SHOW DATABASES;" | grep -Ev "^(Database|information_schema|performance_schema|mysql|sys)$")

for db in $databases; do
backup_file="$BACKUP_DIR/${db}_$(date +%Y%m%d_%H%M%S).sql.gz"

log_message "Backing up database: $db"

if mysqldump -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASS" \
--single-transaction \
--routines \
--triggers \
"$db" | gzip > "$backup_file"; then

log_message "Successfully backed up $db to $backup_file"

# Verify backup
if [ -s "$backup_file" ]; then
size=$(stat -c%s "$backup_file")
log_message "Backup file size: $size bytes"
else
log_message "ERROR: Backup file is empty"
fi
else
log_message "ERROR: Failed to backup $db"
fi
done

# Cleanup old backups
log_message "Cleaning up old backups (older than $RETENTION_DAYS days)..."
find "$BACKUP_DIR" -name "*.sql.gz" -mtime +$RETENTION_DAYS -delete

log_message "MySQL backup completed"

# Disk space check
backup_size=$(du -sh "$BACKUP_DIR" | cut -f1)
log_message "Total backup directory size: $backup_size"

Log Analysis Automation

#!/bin/bash
# log-analysis.sh - Automated log analysis and alerting

LOG_DIR="/var/log"
REPORT_FILE="/tmp/log-analysis-$(date +%Y%m%d).txt"
ALERT_THRESHOLD=10

# Function to analyze log files
analyze_logs() {
echo "=== Log Analysis Report - $(date) ===" > "$REPORT_FILE"
echo >> "$REPORT_FILE"

# Check authentication logs
echo "=== Authentication Analysis ===" >> "$REPORT_FILE"
failed_logins=$(grep "Failed password" /var/log/auth.log | wc -l)
echo "Failed login attempts: $failed_logins" >> "$REPORT_FILE"

if [ "$failed_logins" -gt "$ALERT_THRESHOLD" ]; then
echo "ALERT: High number of failed login attempts!" >> "$REPORT_FILE"
echo "Recent failed attempts:" >> "$REPORT_FILE"
grep "Failed password" /var/log/auth.log | tail -10 >> "$REPORT_FILE"
fi

# Check system errors
echo -e "\n=== System Error Analysis ===" >> "$REPORT_FILE"
error_count=$(grep -i error /var/log/syslog | wc -l)
echo "System errors: $error_count" >> "$REPORT_FILE"

if [ "$error_count" -gt 0 ]; then
echo "Recent errors:" >> "$REPORT_FILE"
grep -i error /var/log/syslog | tail -5 >> "$REPORT_FILE"
fi

# Check disk space
echo -e "\n=== Disk Space Analysis ===" >> "$REPORT_FILE"
df -h | awk '$5 > 80 {print "WARNING: " $0}' >> "$REPORT_FILE"

# Service status
echo -e "\n=== Service Status ===" >> "$REPORT_FILE"
systemctl --failed --no-legend >> "$REPORT_FILE"

# Mail report
if command -v mail >/dev/null 2>&1; then
mail -s "Daily Log Analysis - $(hostname)" admin@company.com < "$REPORT_FILE"
fi
}

# Crontab entry:
# 0 6 * * * /usr/local/bin/log-analysis.sh

analyze_logs

Bu tutorial cron jobs va task scheduling bo'yicha to'liq ma'lumot beradi - asosiy cron sintaksisidan tortib zamonaviy systemd timer'lar va complex automation script'lari bilan birga.