Linux Tutorials

Scheduling Tasks with Cron Jobs on Linux

Automate backups, updates, and scripts on a schedule with Linux's built-in cron scheduler.

Published Apr 1, 2025Updated Mar 15, 20267 min readBeginner
Table of Contents

What is Cron?

Cron is a time-based job scheduler built into every Linux server. You specify when and what to run in a crontab file, and cron handles the rest automatically.

Crontab Syntax

# ┌──── minute (0-59)
# │ ┌──── hour (0-23)
# │ │ ┌──── day of month (1-31)
# │ │ │ ┌──── month (1-12)
# │ │ │ │ ┌──── day of week (0-7, 0=Sunday, 7=Sunday)
# │ │ │ │ │
# * * * * *  command_to_execute

Common Schedule Examples

# Every minute
* * * * * /path/to/script.sh

# Every hour (at :00)
0 * * * * /path/to/script.sh

# Every day at 2:30 AM
30 2 * * * /path/to/backup.sh

# Every Monday at 9:00 AM
0 9 * * 1 /path/to/weekly-report.sh

# Every 5 minutes
*/5 * * * * /path/to/check.sh

# First day of every month at midnight
0 0 1 * * /path/to/monthly.sh

Managing Crontabs

crontab -e    # Edit your crontab (opens in nano/vim)
crontab -l    # List your cron jobs
crontab -r    # Remove ALL your cron jobs (careful!)
crontab -u username -l  # View another user's crontab

Log Cron Output

# Redirect all output to a log file
0 2 * * * /opt/backup.sh >> /var/log/backup.log 2>&1

# Discard all output (silent)
0 2 * * * /opt/backup.sh > /dev/null 2>&1

Real-World Backup Script

cat > /opt/daily-backup.sh << 'EOF'
#!/bin/bash
DATE=$(date +%Y-%m-%d)
BACKUP_DIR="/backups"
mkdir -p "$BACKUP_DIR"

# Backup MySQL
mysqldump -u root myapp > "$BACKUP_DIR/myapp-$DATE.sql"

# Keep only last 7 backups
find "$BACKUP_DIR" -name "*.sql" -mtime +7 -delete

echo "Backup complete: $DATE"
EOF
chmod +x /opt/daily-backup.sh

# Schedule at 3 AM daily
(crontab -l; echo "0 3 * * * /opt/daily-backup.sh >> /var/log/backup.log 2>&1") | crontab -
💡 Use crontab.guru to visually validate your cron schedule before deploying it.
cronautomationLinuxscheduler
Was this article helpful?

Join Our Discord

Connect with our community of gamers and developers

Get instant support, share experiences, and stay updated with the latest news

Join Us On Discord
2026 VPS SaleLIMITED TIME
View Sale Plans