Cron Expression: Every 4 Hours (0 */4 * * *)
Need to generate a cron expression?
Use CronOS to generate any cron expression you wish with natural language. Simply describe what you need, and we'll create the perfect cron expression for you. It's completely free!
Cron Expression: Every 4 Hours (0 */4 * * *)
The cron expression 0 */4 * * * executes a task every 4 hours at the top of the hour (minute 0), making it suitable for periodic backups, data synchronization, and maintenance operations.
Expression Breakdown
0 */4 * * *
│ │ │ │ │
│ │ │ │ └─── Day of week: * (every day)
│ │ │ └───── Month: * (every month)
│ │ └─────── Day of month: * (every day)
│ └────────── Hour: */4 (every 4 hours)
└───────────── Minute: 0 (at minute 0)
Field Values
| Field | Value | Meaning |
|---|---|---|
| Minute | 0 | At minute 0 (top of the hour) |
| Hour | */4 | Every 4 hours (0, 4, 8, 12, 16, 20) |
| Day of Month | * | Every day (1-31) |
| Month | * | Every month (1-12) |
| Day of Week | * | Every day of week (0-7) |
Step Value Syntax
The /4 in the hour field is a step value that means "every 4th hour starting from 0":
- Runs at: 00:00, 04:00, 08:00, 12:00, 16:00, 20:00
Common Use Cases
1. Periodic Backups
0 */4 * * * /usr/local/bin/backup.sh
Create backups or snapshots of databases and critical files every 4 hours.
2. Data Synchronization
0 */4 * * * /usr/bin/python3 /scripts/sync-data.py
Sync data between systems, databases, or external services.
3. Cache Refresh
0 */4 * * * /usr/bin/python3 /scripts/refresh-cache.py
Refresh cached data, computed statistics, or API responses.
4. Health Monitoring
0 */4 * * * /usr/local/bin/system-health-check.sh
Monitor system health, resource usage, or service availability.
Execution Frequency
This expression runs 6 times per day at:
- 00:00, 04:00, 08:00, 12:00, 16:00, 20:00
Example Implementations
Backup Script
#!/bin/bash
# /usr/local/bin/backup.sh
BACKUP_DIR="/var/backups/4hourly"
SOURCE_DIR="/var/data"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
LOG_FILE="/var/log/backups.log"
mkdir -p $BACKUP_DIR
tar -czf "$BACKUP_DIR/backup_$TIMESTAMP.tar.gz" \
-C $(dirname $SOURCE_DIR) \
$(basename $SOURCE_DIR) >> $LOG_FILE 2>&1
find $BACKUP_DIR -name "*.tar.gz" -mtime +14 -delete
echo "$(date): 4-hourly backup completed" >> $LOG_FILE
Python Data Sync
# sync-data.py
import requests
import json
from datetime import datetime
import sqlite3
def sync_data():
try:
response = requests.get(
'https://api.external.com/data',
timeout=180,
headers={'Authorization': 'Bearer YOUR_TOKEN'}
)
response.raise_for_status()
data = response.json()
conn = sqlite3.connect('/var/data/app.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS synced_data (
id TEXT PRIMARY KEY,
data TEXT,
updated_at TIMESTAMP
)
''')
for item in data:
cursor.execute('''
INSERT OR REPLACE INTO synced_data
(id, data, updated_at)
VALUES (?, ?, ?)
''', (item['id'], json.dumps(item), datetime.now()))
conn.commit()
conn.close()
print(f"{datetime.now()}: Synced {len(data)} records")
except Exception as e:
print(f"{datetime.now()}: Sync failed: {e}")
if __name__ == '__main__':
sync_data()
Best Practices
- Execution Time: Tasks should complete within 230-235 minutes
- Locking: Use file locks or distributed locks to prevent concurrent execution
- Error Handling: Implement comprehensive error handling and logging
- Idempotency: Design tasks to be safely re-runnable
- Resource Management: Monitor CPU, memory, and I/O usage
When to Use
✅ Good for:
- Periodic backups
- Data synchronization
- Cache refresh operations
- Health monitoring
- Less frequent maintenance tasks
❌ Avoid for:
- Real-time critical operations
- Tasks requiring immediate execution
- Very long-running processes (over 230 minutes)
Comparison with Other Intervals
| Interval | Expression | Runs/Day | Best For |
|---|---|---|---|
| Every 3 hours | 0 */3 * * * | 8 | More frequent tasks |
| Every 4 hours | 0 */4 * * * | 6 | Periodic tasks |
| Every 6 hours | 0 */6 * * * | 4 | Less frequent tasks |
| Every 8 hours | 0 */8 * * * | 3 | Even less frequent |
Conclusion
The 0 */4 * * * expression is suitable for tasks that need regular execution but can tolerate a 4-hour interval. It's perfect for backups, data synchronization, and maintenance operations that don't require more frequent execution.
Need to generate a cron expression?
Use CronOS to generate any cron expression you wish with natural language. Simply describe what you need, and we'll create the perfect cron expression for you. It's completely free!