Back to Home

Cron Expression: Every 5 Minutes (*/5 * * * *)

CronOS Team
cronschedulingevery-5-minutesmonitoringtutorial

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!

Generate Cron Expression

Cron Expression: Every 5 Minutes (*/5 * * * *)

The cron expression */5 * * * * executes a task every 5 minutes, making it one of the most popular intervals for regular monitoring, data collection, and periodic maintenance tasks.

Expression Breakdown

bash
*/5 * * * *
│   │ │ │ │
│   │ │ │ └─── Day of week: * (every day)
│   │ │ └───── Month: * (every month)
│   │ └─────── Day of month: * (every day)
│   └───────── Hour: * (every hour)
└───────────── Minute: */5 (every 5 minutes)

Field Values

FieldValueMeaning
Minute*/5Every 5 minutes (0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55)
Hour*Every hour (0-23)
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 /5 is a step value that means "every 5th minute starting from 0":

  • Runs at: 00:00, 00:05, 00:10, 00:15, 00:20, 00:25, 00:30, 00:35, 00:40, 00:45, 00:50, 00:55
  • Then repeats every hour: 01:00, 01:05, 01:10, and so on

Common Use Cases

1. System Monitoring

bash
*/5 * * * * /usr/local/bin/system-monitor.sh

Monitor system resources, disk space, memory usage, and CPU load.

2. Database Backups

bash
*/5 * * * * /usr/local/bin/incremental-backup.sh

Create incremental backups or snapshots of critical data.

3. Cache Refresh

bash
*/5 * * * * /usr/bin/python3 /scripts/refresh-cache.py

Refresh cached data, API responses, or computed values.

4. Log Rotation

bash
*/5 * * * * /usr/local/bin/rotate-logs.sh

Rotate or archive log files to prevent disk space issues.

5. Queue Processing

bash
*/5 * * * * /usr/bin/node /app/process-queue.js

Process background jobs, email queues, or message queues.

Execution Frequency

This expression runs 12 times per hour (288 times per day) at:

  • :00, :05, :10, :15, :20, :25, :30, :35, :40, :45, :50, :55 of every hour

Example Implementations

System Monitoring Script

bash
#!/bin/bash
# /usr/local/bin/system-monitor.sh

LOG_FILE="/var/log/system-monitor.log"
THRESHOLD_CPU=80
THRESHOLD_MEM=85
THRESHOLD_DISK=90

# Check CPU usage
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1)
if (( $(echo "$CPU_USAGE > $THRESHOLD_CPU" | bc -l) )); then
    echo "$(date): WARNING - CPU usage at ${CPU_USAGE}%" >> $LOG_FILE
fi

# Check memory usage
MEM_USAGE=$(free | grep Mem | awk '{printf "%.0f", $3/$2 * 100}')
if [ $MEM_USAGE -gt $THRESHOLD_MEM ]; then
    echo "$(date): WARNING - Memory usage at ${MEM_USAGE}%" >> $LOG_FILE
fi

# Check disk usage
DISK_USAGE=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')
if [ $DISK_USAGE -gt $THRESHOLD_DISK ]; then
    echo "$(date): WARNING - Disk usage at ${DISK_USAGE}%" >> $LOG_FILE
fi

Python Cache Refresh

python
# refresh-cache.py
import redis
import requests
from datetime import datetime

def refresh_cache():
    r = redis.Redis(host='localhost', port=6379, db=0)
    
    try:
        # Fetch fresh data
        response = requests.get('https://api.example.com/data', timeout=10)
        data = response.json()
        
        # Update cache
        r.setex('cached_data', 300, str(data))  # 5 min TTL
        
        print(f"{datetime.now()}: Cache refreshed successfully")
    except Exception as e:
        print(f"{datetime.now()}: Cache refresh failed: {e}")

if __name__ == '__main__':
    refresh_cache()

Node.js Queue Processor

javascript
// process-queue.js
const Queue = require('bull');
const queue = new Queue('email', {
  redis: { host: 'localhost', port: 6379 }
});

async function processEmailQueue() {
  const jobs = await queue.getWaiting({ limit: 50 });
  
  console.log(`Processing ${jobs.length} email jobs`);
  
  for (const job of jobs) {
    try {
      await job.process();
      console.log(`Processed email job ${job.id}`);
    } catch (error) {
      console.error(`Email job ${job.id} failed:`, error);
      await job.moveToFailed(error);
    }
  }
}

processEmailQueue().catch(console.error);

Best Practices

  1. Execution Window: Tasks should complete within 4-5 minutes to avoid overlap
  2. Idempotency: Design tasks to be safely re-runnable
  3. Error Handling: Implement comprehensive logging and alerting
  4. Resource Management: Monitor and limit resource consumption
  5. Locking: Use file locks or distributed locks to prevent concurrent execution

When to Use

Good for:

  • System and application monitoring
  • Incremental backups
  • Cache refresh operations
  • Queue processing
  • Log rotation
  • Data synchronization
  • Health checks

Avoid for:

  • Real-time critical operations (use shorter intervals)
  • Very long-running processes
  • Tasks requiring more than 4 minutes
  • Heavy database operations without optimization

Comparison with Other Intervals

IntervalExpressionRuns/HourBest For
Every 2 minutes*/2 * * * *30Frequent monitoring
Every 5 minutes*/5 * * * *12Regular monitoring
Every 10 minutes*/10 * * * *6Periodic tasks
Every 15 minutes*/15 * * * *4Standard intervals

Real-World Example

A common pattern for monitoring a web application:

bash
# Monitor application health
*/5 * * * * /usr/local/bin/check-app-health.sh

# Process email queue
*/5 * * * * /usr/bin/node /app/process-email-queue.js

# Refresh API cache
*/5 * * * * /usr/bin/python3 /scripts/refresh-api-cache.py

Conclusion

The */5 * * * * expression is ideal for regular monitoring and maintenance tasks. It provides a good balance between responsiveness and resource efficiency, making it one of the most commonly used cron intervals in production environments.

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!

Generate Cron Expression