Cron Expression: Every 2 Minutes (*/2 * * * *)
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 2 Minutes (*/2 * * * *)
The cron expression */2 * * * * executes a task every 2 minutes, providing a balance between real-time monitoring and system resource usage.
Expression Breakdown
*/2 * * * *
│ │ │ │ │
│ │ │ │ └─── Day of week: * (every day)
│ │ │ └───── Month: * (every month)
│ │ └─────── Day of month: * (every day)
│ └───────── Hour: * (every hour)
└───────────── Minute: */2 (every 2 minutes)
Field Values
| Field | Value | Meaning |
|---|---|---|
| Minute | */2 | Every 2 minutes (0, 2, 4, 6, ..., 58) |
| 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 /2 is a step value that means "every 2nd minute starting from 0":
- Runs at: 00:00, 00:02, 00:04, 00:06, ..., 00:58
- Then: 01:00, 01:02, 01:04, ..., and so on
Common Use Cases
1. Application Health Monitoring
*/2 * * * * /usr/local/bin/check-app-health.sh
Monitor application status, API endpoints, or service availability every 2 minutes.
2. Data Synchronization
*/2 * * * * /usr/bin/python3 /scripts/sync-data.py
Sync data between systems, databases, or services at regular intervals.
3. Log File Monitoring
*/2 * * * * /usr/bin/tail -n 100 /var/log/app.log | grep ERROR
Check for errors or specific patterns in log files frequently.
4. Queue Processing
*/2 * * * * /usr/bin/node /app/process-queue.js
Process job queues or message queues at regular intervals.
Execution Times
This expression runs 30 times per hour at these minutes:
- 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58
Example Implementations
Health Check Script
#!/bin/bash
# /usr/local/bin/check-app-health.sh
APP_URL="https://api.example.com/health"
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" $APP_URL)
if [ $RESPONSE -ne 200 ]; then
echo "$(date): Health check failed with status $RESPONSE" | logger -t health-check
# Optional: Send alert
/usr/local/bin/send-alert.sh "App health check failed"
fi
Python Data Sync
# sync-data.py
import requests
import json
from datetime import datetime
def sync_data():
try:
# Fetch data from source
response = requests.get('https://api.source.com/data', timeout=30)
data = response.json()
# Process and save
with open('/var/data/latest.json', 'w') as f:
json.dump(data, f)
print(f"{datetime.now()}: Data synced successfully")
except Exception as e:
print(f"{datetime.now()}: Sync failed: {e}")
if __name__ == '__main__':
sync_data()
Node.js Queue Processor
// process-queue.js
const Queue = require('bull');
const queue = new Queue('tasks', {
redis: { host: 'localhost', port: 6379 }
});
async function processQueue() {
const jobs = await queue.getWaiting();
for (const job of jobs.slice(0, 10)) { // Process up to 10 jobs
try {
await job.process();
console.log(`Processed job ${job.id}`);
} catch (error) {
console.error(`Job ${job.id} failed:`, error);
}
}
}
processQueue().catch(console.error);
Best Practices
- Execution Time: Keep tasks under 60 seconds to avoid overlap
- Error Handling: Log failures and implement retry logic
- Idempotency: Ensure tasks can run multiple times safely
- Resource Limits: Monitor CPU, memory, and I/O usage
- Rate Limiting: Respect external API rate limits
When to Use
✅ Good for:
- Frequent health checks
- Data synchronization
- Queue processing
- Log monitoring
- Status updates
❌ Avoid for:
- Heavy computations
- Long-running processes
- Tasks requiring more than 60 seconds
- Database-intensive operations
Comparison with Other Intervals
| Interval | Expression | Runs/Hour | Use Case |
|---|---|---|---|
| Every minute | * * * * * | 60 | Real-time monitoring |
| Every 2 minutes | */2 * * * * | 30 | Frequent checks |
| Every 5 minutes | */5 * * * * | 12 | Regular monitoring |
| Every 15 minutes | */15 * * * * | 4 | Periodic tasks |
Conclusion
The */2 * * * * expression provides a good balance for frequent monitoring and synchronization tasks. It's less resource-intensive than every-minute execution while still maintaining near real-time responsiveness for critical operations.
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!