⏰
Hosting

Complete Cron Job Guide: Scheduling Automatic Tasks

30.05.2030
← All articles

Cron is the system for scheduling automated tasks on Unix and Linux servers. Created by Brian Kernighan at AT&T Bell Labs in 1975. Cron runs every minute and checks its schedule β€” if a scheduled time matches it runs the corresponding script. You'll need this for site backups, email sending, database cleanup, and similar recurring tasks.

Cron syntax

Cron job syntax has five fields: minute (0-59), hour (0-23), day of month (1-31), month (1-12), day of week (0-7, both 0 and 7 = Sunday), then the command to run.

Examples: * * * * * /path/to/script.sh β€” every minute; 0 * * * * /path/to/script.sh β€” at the top of every hour; 0 0 * * * /path/to/script.sh β€” every day at midnight; 0 0 * * 0 /path/to/script.sh β€” every Sunday at midnight; */5 * * * * /path/to/script.sh β€” every 5 minutes.

Adding a cron job in cPanel

cPanel β†’ Cron Jobs β†’ "Add New Cron Job". Pick a preset under Common Settings or set a custom time. Enter the command to run in the Command field. Example: WordPress wp-cron.php every 15 minutes: */15 * * * * /usr/local/bin/php /home/user/public_html/wp-cron.php

Real examples

Automate WordPress wp-cron β€” native WordPress cron runs on every page load (slow). Better to run it via real cron every 15 minutes. First add to wp-config.php: define('DISABLE_WP_CRON', true);, then create the cron.

Daily database backup β€” 0 2 * * * mysqldump -u dbuser -pPASSWORD dbname | gzip > /home/user/backups/db_$(date +\%Y\%m\%d).sql.gz. The % must be escaped as \%.

Send a weekly email report on Monday morning: 0 9 * * 1 /usr/local/bin/php /home/user/scripts/weekly_report.php.

Clean log files on the 1st of every month: 0 0 1 * * find /home/user/logs -name "*.log" -mtime +30 -delete.

Debugging

If cron doesn't run, common causes: (1) Path issue β€” incomplete script path; (2) Permission issue β€” script doesn't have execute permission (chmod +x); (3) Missing shebang β€” no #!/bin/bash or #!/usr/bin/php at the top; (4) Environment β€” cron's environment differs from shell, $PATH and others are different.

For debugging, redirect output to a log file: * * * * * /path/to/script.sh > /home/user/cron.log 2>&1.

Sayt.uz practice

Cron jobs are enabled by default on Sayt.uz hosting plans. Basic Hosting has 5 cron jobs; Pro Hosting unlimited. 67% of clients use at least one cron. Most common uses: WordPress wp-cron (47%), database backup (32%), email/Telegram notifications (28%), log cleanup (19%). Tip: always write cron scripts with absolute paths (/home/user/script.sh, not ./script.sh) β€” this is the single biggest source of errors.

Related articles

πŸ’° Hosting Price Comparison: Uzbek and International Providers πŸ“‘ Server Monitoring Tools: Prometheus, Grafana, Datadog, and More 🌐 Edge Computing Hosting: Moving Compute Closer to Users 🏒 Colocation Server: Placing Your Own Hardware in a Data Center
🌐 Language
πŸ‡ΊπŸ‡Ώ O'zbek πŸ‡ΊπŸ‡Ώ ЎзбСк πŸ‡·πŸ‡Ί Русский πŸ‡¬πŸ‡§ English βœ“