Back to Blog
Tutorial

Cron Job Every Hour: Complete Guide with Examples (2025)

Cron Job Every Hour: Complete Guide with Examples (2025)

Need to run a task once per hour? Hourly cron jobs are perfect for regular maintenance, data syncs, backups, and monitoring.

This guide shows you how to set up cron jobs that run every hour with practical examples for all scenarios.

Quick Answer: Cron Expression for Every Hour

0 * * * * /path/to/your/script.sh

Breakdown:

  • 0 = At minute 0 (top of the hour)
  • * = Every hour
  • * = Every day
  • * = Every month
  • * = Every day of week

Result: Runs at 00:00, 01:00, 02:00... 23:00 (24 times per day)


Alternative: Run at Different Minute

Want to run at 15 minutes past each hour?

15 * * * * /path/to/script.sh

Runs at: 00:15, 01:15, 02:15... 23:15


How to Set Up an Hourly Cron Job

Step 1: Open Your Crontab

crontab -e

Step 2: Add the Hourly Job

0 * * * * /home/user/scripts/hourly-backup.sh

Step 3: Save and Exit

  • Nano: Press Ctrl+X, then Y, then Enter
  • Vim: Press Esc, type :wq, press Enter

Step 4: Verify

crontab -l

Real-World Examples

Example 1: Database Backup Every Hour

0 * * * * /usr/bin/mysqldump -u root -pPASSWORD mydb > /backups/hourly/mydb-$(date +\%H).sql

What it does:

  • Creates MySQL backup
  • Runs at top of each hour
  • Uses hour (00-23) in filename
  • Overwrites previous hour's backup (keeps 24 backups)

Example 2: Log Rotation Every Hour

0 * * * * find /var/log/myapp -name "*.log" -mtime +7 -delete

What it does:

  • Deletes logs older than 7 days
  • Runs hourly to keep disk space under control

Example 3: PHP Script Every Hour

0 * * * * /usr/bin/php /var/www/html/hourly-sync.php >> /var/log/sync.log 2>&1

Example 4: Python Data Processing

0 * * * * cd /home/user/analytics && /home/user/analytics/venv/bin/python process_data.py

Example 5: Fetch External Data

0 * * * * curl -s https://api.example.com/data > /tmp/hourly-data.json

Example 6: Node.js Report Generation

0 * * * * /usr/bin/node /home/user/app/generate-report.js

Business Hours Only

Run Every Hour from 9 AM to 5 PM

0 9-17 * * * /path/to/script.sh

Runs at: 09:00, 10:00, 11:00... 17:00 (9 times per day)

Weekdays Only (Monday-Friday)

0 9-17 * * 1-5 /path/to/script.sh

Runs: Hourly during business hours, weekdays only


Every N Hours

Every 2 Hours

0 */2 * * * /path/to/script.sh

Runs at: 00:00, 02:00, 04:00, 06:00... 22:00 (12 times per day)

Every 3 Hours

0 */3 * * * /path/to/script.sh

Runs at: 00:00, 03:00, 06:00, 09:00, 12:00, 15:00, 18:00, 21:00 (8 times/day)

Every 4 Hours

0 */4 * * * /path/to/script.sh

Runs at: 00:00, 04:00, 08:00, 12:00, 16:00, 20:00 (6 times/day)

Every 6 Hours

0 */6 * * * /path/to/script.sh

Runs at: 00:00, 06:00, 12:00, 18:00 (4 times/day)

Every 12 Hours

0 */12 * * * /path/to/script.sh

Runs at: 00:00, 12:00 (2 times/day)


Specific Hours Only

Run at Specific Hours (e.g., 9 AM, 1 PM, 5 PM)

0 9,13,17 * * * /path/to/script.sh

Night Hours Only (11 PM to 5 AM)

0 23,0-5 * * * /path/to/script.sh

Interval Comparison Table

| Cron Expression | Description | Runs Per Day | |----------------|-------------|--------------| | 0 * * * * | Every hour | 24 | | 0 */2 * * * | Every 2 hours | 12 | | 0 */3 * * * | Every 3 hours | 8 | | 0 */4 * * * | Every 4 hours | 6 | | 0 */6 * * * | Every 6 hours | 4 | | 0 */12 * * * | Every 12 hours | 2 | | 0 0 * * * | Once daily (midnight) | 1 | | 0 9-17 * * * | Every hour, 9 AM-5 PM | 9 |


Common Mistakes & Fixes

❌ Wrong: Missing Minute Field

* * * * * /script.sh  # Runs EVERY MINUTE!

Correct:

0 * * * * /script.sh  # Runs every hour

❌ Wrong: Using Step on Wrong Field

*/1 0 * * * /script.sh  # Only runs at midnight!

Correct:

0 */1 * * * /script.sh  # Runs every hour

❌ Wrong: Relative Path

0 * * * * ~/scripts/backup.sh  # Won't work in cron

Correct:

0 * * * * /home/user/scripts/backup.sh

Add Logging

Always log hourly cron jobs for debugging:

0 * * * * /path/to/script.sh >> /var/log/hourly-cron.log 2>&1

View logs:

tail -f /var/log/hourly-cron.log

Prevent Overlapping Executions

If your hourly script might take > 1 hour:

0 * * * * flock -n /tmp/hourly.lock -c '/path/to/script.sh'

How it works:

  • flock creates lock file
  • -n = skip if already running
  • Prevents multiple instances

Troubleshooting Hourly Cron Jobs

1. Check Cron Service

sudo systemctl status cron

Start if stopped:

sudo systemctl start cron

2. Check Cron Logs

Ubuntu/Debian:

grep CRON /var/log/syslog | grep hourly

CentOS/RHEL:

tail -100 /var/log/cron

3. Test Script Manually

/path/to/your/script.sh

4. Check Permissions

ls -l /path/to/script.sh
chmod +x /path/to/script.sh

5. Set Environment Variables

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
SHELL=/bin/bash

0 * * * * /path/to/script.sh

Real-World Hourly Cron Job Use Cases

1. Weather Data Updates

0 * * * * curl -s "https://api.weather.com/data?city=london" > /var/www/html/weather.json

2. Sitemap Generation

0 * * * * cd /var/www/html && php artisan sitemap:generate

3. Database Optimization

0 3 * * * /usr/bin/mysqlcheck -u root -pPASSWORD --optimize --all-databases

Runs at 3 AM daily.

4. Cache Clearing

0 * * * * rm -rf /tmp/cache/*

5. SSL Certificate Renewal Check

0 */12 * * * certbot renew --quiet

Checks twice daily.

6. Git Pull Updates

0 * * * * cd /var/www/html && git pull origin main >> /var/log/git-pull.log 2>&1

7. Stock Market Data (Trading Hours)

0 9-16 * * 1-5 /home/trader/fetch-stock-data.py

Every hour during market hours, weekdays.


Monitoring Hourly Cron Jobs

Running critical hourly tasks? Monitor them:


System-Wide vs User Cron

User Crontab (Most Common)

crontab -e
0 * * * * /home/user/script.sh

Runs as your user.

System Crontab (/etc/crontab)

sudo nano /etc/crontab

Add:

0 * * * * root /path/to/script.sh

Must specify username.

Hourly Directory (/etc/cron.hourly)

Place executable scripts here:

sudo nano /etc/cron.hourly/backup-script
chmod +x /etc/cron.hourly/backup-script

Note: Scripts in /etc/cron.hourly run via anacron, not exactly on the hour.


Notification on Completion

Send email when hourly job completes:

0 * * * * /path/to/script.sh && echo "Hourly backup completed" | mail -s "Success" admin@example.com

Or on failure:

0 * * * * /path/to/script.sh || echo "Hourly backup failed!" | mail -s "ERROR" admin@example.com

Different Start Times

Run at :30 Past Each Hour

30 * * * * /path/to/script.sh

Runs at: 00:30, 01:30, 02:30... 23:30

Run at :15 and :45

15,45 * * * * /path/to/script.sh

Runs: Twice per hour (48 times per day)


Performance Considerations

Hourly vs More Frequent

| Frequency | Expression | Runs/Day | Best For | |-----------|------------|----------|----------| | Every minute | * * * * * | 1,440 | Real-time monitoring | | Every 5 min | */5 * * * * | 288 | API health checks | | Every 15 min | */15 * * * * | 96 | Queue processing | | Every hour | 0 * * * * | 24 | Backups, syncs | | Every 6 hours | 0 */6 * * * | 4 | Large data updates | | Daily | 0 0 * * * | 1 | Reports, analytics |

Hourly is ideal for:

  • ✅ Regular backups
  • ✅ Data synchronization
  • ✅ Log rotation
  • ✅ Cache updates
  • ✅ Scheduled reports

Quick Reference: Hourly Cron Jobs

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

# Every hour at :30
30 * * * * /path/to/script.sh

# Every 2 hours
0 */2 * * * /path/to/script.sh

# Business hours (9 AM - 5 PM)
0 9-17 * * * /path/to/script.sh

# Weekdays only
0 * * * 1-5 /path/to/script.sh

# With logging
0 * * * * /path/to/script.sh >> /var/log/cron.log 2>&1

# Prevent overlap
0 * * * * flock -n /tmp/hourly.lock /path/to/script.sh

# PHP
0 * * * * /usr/bin/php /var/www/cron.php

# Python
0 * * * * /usr/bin/python3 /home/user/script.py

# Node.js
0 * * * * /usr/bin/node /home/user/app.js

# With email on error
0 * * * * /path/to/script.sh || mail -s "Error" admin@example.com

Need Reliable Hosting?

Running hourly cron jobs requires stable infrastructure:


Disable/Remove Hourly Cron Job

Temporarily Disable

crontab -e

Comment out:

# 0 * * * * /path/to/script.sh

Permanently Remove

crontab -e

Delete the line completely.


Testing Hourly Cron Jobs

Method 1: Change to Every Minute

* * * * * /path/to/script.sh

Watch it run every minute, verify it works, then change back to 0 * * * *.

Method 2: Run at Next Hour

0 15 * * * /path/to/script.sh

Set to 3 PM if it's currently 2:50 PM, wait, observe, then change to hourly.

Method 3: Manual Test

/path/to/script.sh

Conclusion

Setting up hourly cron jobs is simple:

0 * * * * /path/to/your/script.sh

Key Takeaways:

  • ✅ Runs 24 times per day (once per hour)
  • ✅ Use 0 for minute field to run at top of hour
  • ✅ Or use any minute (0-59) for custom timing
  • ✅ Use */N for every N hours
  • ✅ Always use absolute paths
  • ✅ Add logging for debugging
  • ✅ Use flock to prevent overlaps

Next Steps:

  1. Create custom cron schedules
  2. Decode complex cron expressions
  3. Save and organize your cron jobs

Related Articles:

Keywords: cron job every hour, hourly cron, crontab every hour, run script hourly, linux hourly cron, cron expression every hour, ubuntu hourly cron job