⏰ Scheduling and Automation
Repetitive tasks should be handled by machines. Through Cron scheduling and Heartbeat periodic checks, let your AI partner become a tireless automation assistant.
Cron Scheduling Basics
What is Cron?
Cron is a task scheduler for Linux/Unix systems. You can set “when to execute what commands,” and the system will automatically run them on schedule.
Cron Syntax Format
┌───────────── Minutes (0 - 59)
│ ┌───────────── Hours (0 - 23)
│ │ ┌───────────── Day of month (1 - 31)
│ │ │ ┌───────────── Month (1 - 12)
│ │ │ │ ┌───────────── Day of week (0 - 6, 0=Sunday)
│ │ │ │ │
│ │ │ │ │
* * * * * Command to execute
Common Cron Examples
{`# Execute every day at 8 AM
0 8 * * * /path/to/script.sh
# Execute every hour
0 * * * * /path/to/script.sh
# Execute every 15 minutes
*/15 * * * * /path/to/script.sh
# Execute every Monday at 9 AM
0 9 * * 1 /path/to/script.sh
# Execute at 3 AM on the 1st of every month
0 3 1 * * /path/to/script.sh
# Execute every hour on weekdays (Monday-Friday)
0 * * * 1-5 /path/to/script.sh
# Execute at 9 AM, 12 PM, and 6 PM daily
0 9,12,18 * * * /path/to/script.sh`}
Setting Up Cron Jobs
{`# 1. Edit crontab file
crontab -e
# 2. Add your schedule (example: daily database backup)
0 2 * * * cd /home/node/.openclaw/workspace && ./scripts/backup-db.sh
# 3. View current scheduled tasks
crontab -l
# 4. Delete all tasks (use with caution!)
crontab -r`}
Heartbeat Periodic Checks
What is Heartbeat?
Heartbeat is RealVco’s periodic health check mechanism. Unlike Cron’s “execute at fixed times,” Heartbeat is “check status periodically, act only when needed.”
Heartbeat vs Cron
| Feature | Cron | Heartbeat |
|---|---|---|
| Execution Method | Execute at fixed times | Check status before deciding to act |
| Suitable Scenarios | Backups, report sending | Monitoring, health checks |
| Flexibility | Fixed timing | Can adjust based on status |
| Resource Consumption | Fixed | Only consumes when issues exist |
Heartbeat Workflow
┌─────────────────────────────────────────────────────────┐
│ Heartbeat Cycle │
├─────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────┐ │
│ │ Start Check │ ◄──────────────────────────┐ │
│ └──────┬──────┘ │ │
│ │ │ │
│ ▼ │ │
│ ┌─────────────┐ Normal ┌─────────┐ │ │
│ │ Check Status│ ────────────→ │ Log │ │ │
│ └──────┬──────┘ └────┬────┘ │ │
│ │ │ │ │
│ │ Abnormal │ │ │
│ ▼ │ │ │
│ ┌─────────────┐ │ │ │
│ │ Execute │ │ │ │
│ │ Response │ │ │ │
│ └──────┬──────┘ │ │ │
│ │ │ │ │
│ ▼ │ │ │
│ ┌─────────────┐ │ │ │
│ │ Notify │ │ │ │
│ │ Admin │ │ │ │
│ └─────────────┘ │ │ │
│ │ │ │
│ └──────────────────────────────────┘ │ │
│ Wait for next schedule (~30 min) │ │
│ │ │
└────────────────────────────────────────────────┘ │
│ │
└──────────────────────────────┘
Heartbeat Configuration File
{`# HEARTBEAT.md - Periodic Check Task List
## System Resource Check
Frequency: Every 2 hours
Tasks:
- [ ] Check disk space usage (warning threshold: 80%)
- [ ] Check memory usage (warning threshold: 90%)
- [ ] Check CPU load
- [ ] Record check results to log
## Service Health Check
Frequency: Every 30 minutes
Tasks:
- [ ] Check Nginx service status
- [ ] Check database connection
- [ ] Check Docker container status
- [ ] Check SSL certificate expiration
## Project Progress Check
Frequency: 4 times daily
Tasks:
- [ ] Check git for uncommitted changes
- [ ] Check for pending work items
- [ ] Update project progress tracking table
## Calendar Reminders
Frequency: Every 5 minutes
Tasks:
- [ ] Check for upcoming meetings
- [ ] Send reminder notifications
- [ ] Preview tomorrow's schedule`}
Heartbeat Status Tracking
{`{
"lastChecks": {
"systemResources": "2026-03-21T08:00:00Z",
"serviceHealth": "2026-03-21T08:30:00Z",
"projectProgress": "2026-03-21T08:00:00Z"
},
"alerts": {
"diskSpace": {
"status": "ok",
"lastAlert": null,
"threshold": 80
},
"memory": {
"status": "warning",
"lastAlert": "2026-03-21T06:00:00Z",
"threshold": 90
}
},
"checkCount": {
"today": 24,
"thisWeek": 168
}
}`}
Common Automation Task Examples
1. Database Backup
{`#!/bin/bash
# Daily database backup script
BACKUP_DIR="/backup/database"
DATE=$(date +%Y%m%d_%H%M%S)
DB_NAME="myapp"
RETENTION_DAYS=7
# Create backup directory
mkdir -p "$BACKUP_DIR"
# Execute backup
echo "Starting database backup..."
mysqldump -u root -p"$DB_PASSWORD" "$DB_NAME" | gzip > "$BACKUP_DIR/db_${DATE}.sql.gz"
# Check if backup was successful
if [ $? -eq 0 ]; then
echo "✅ Backup successful: db_${DATE}.sql.gz"
# Upload to cloud storage (optional)
# aws s3 cp "$BACKUP_DIR/db_${DATE}.sql.gz" s3://my-backup-bucket/
# Send notification
curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" \
-d "chat_id=$ADMIN_CHAT_ID" \
-d "text=✅ Database backup complete: db_${DATE}.sql.gz"
else
echo "❌ Backup failed"
curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" \
-d "chat_id=$ADMIN_CHAT_ID" \
-d "text=🚨 Database backup failed, please check immediately!"
fi
# Clean up old backups (keep 7 days)
find "$BACKUP_DIR" -name "db_*.sql.gz" -mtime +$RETENTION_DAYS -delete
echo "Cleaned up backups older than $RETENTION_DAYS days"`}
2. SSL Certificate Expiration Alert
{`#!/bin/bash
# SSL certificate expiration check
DOMAIN="example.com"
WARN_DAYS=14
# Get certificate expiration date
EXPIRY_DATE=$(echo | openssl s_client -servername "$DOMAIN" -connect "$DOMAIN:443" 2>/dev/null | \
openssl x509 -noout -dates | grep notAfter | cut -d= -f2)
# Calculate days remaining
EXPIRY_TIMESTAMP=$(date -d "$EXPIRY_DATE" +%s)
CURRENT_TIMESTAMP=$(date +%s)
DAYS_UNTIL_EXPIRY=$(( (EXPIRY_TIMESTAMP - CURRENT_TIMESTAMP) / 86400 ))
if [ "$DAYS_UNTIL_EXPIRY" -lt "$WARN_DAYS" ]; then
MESSAGE="⚠️ SSL Certificate Warning\\n\\n"
MESSAGE+="Domain: $DOMAIN\\n"
MESSAGE+="Expiration: $EXPIRY_DATE\\n"
MESSAGE+="Remaining: $DAYS_UNTIL_EXPIRY days\\n\\n"
MESSAGE+="Please renew certificate ASAP!"
curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" \
-d "chat_id=$ADMIN_CHAT_ID" \
-d "text=$MESSAGE"
echo "SSL expiration warning sent"
else
echo "SSL certificate status normal, $DAYS_UNTIL_EXPIRY days remaining"
fi`}
3. Website Health Check
{`#!/bin/bash
# Website health check script
URLS=(
"https://example.com"
"https://api.example.com/health"
"https://admin.example.com"
)
for URL in "${URLS[@]}"; do
# Check HTTP status code
STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$URL")
# Check response time
RESPONSE_TIME=$(curl -s -o /dev/null -w "%{time_total}" "$URL")
if [ "$STATUS" != "200" ]; then
MESSAGE="🚨 Website Alert\\n\\n"
MESSAGE+="URL: $URL\\n"
MESSAGE+="Status: $STATUS\\n"
MESSAGE+="Time: $(date '+%Y-%m-%d %H:%M:%S')\\n\\n"
MESSAGE+="Please check immediately!"
curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" \
-d "chat_id=$ADMIN_CHAT_ID" \
-d "text=$MESSAGE"
elif (( $(echo "$RESPONSE_TIME > 5" | bc -l) )); then
MESSAGE="⚠️ Website Slow\\n\\n"
MESSAGE+="URL: $URL\\n"
MESSAGE+="Response time: ${RESPONSE_TIME}s\\n"
MESSAGE+="Recommend checking server load"
curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" \
-d "chat_id=$ADMIN_CHAT_ID" \
-d "text=$MESSAGE"
else
echo "✅ $URL normal (Status: $STATUS, Response: ${RESPONSE_TIME}s)"
fi
done`}
Configuration Examples
Complete Automation Configuration
{`# Automation task configuration file
schedules:
# Daily backup
daily_backup:
type: cron
schedule: "0 2 * * *" # 2 AM daily
script: "scripts/backup-db.sh"
notify_on: [success, failure]
# Health check
health_check:
type: cron
schedule: "*/30 * * * *" # Every 30 minutes
script: "scripts/health-check.sh"
notify_on: [failure]
# SSL check
ssl_check:
type: cron
schedule: "0 9 * * 1" # Every Monday at 9 AM
script: "scripts/check-ssl.sh"
notify_on: [failure]
# Heartbeat system check
system_heartbeat:
type: heartbeat
interval: 30m
checks:
- disk_space
- memory
- services
notify_on: [warning, critical]
notifications:
telegram:
bot_token: "${TELEGRAM_BOT_TOKEN}"
chat_id: "${ADMIN_CHAT_ID}"
email:
smtp_server: "${SMTP_SERVER}"
to: "${ADMIN_EMAIL}"
retention:
logs: "30d"
backups: "7d"
reports: "90d"`}
Task Execution Log
{`[2026-03-21 02:00:00] INFO: Starting daily_backup
[2026-03-21 02:00:15] INFO: Database backup successful (size: 150MB)
[2026-03-21 02:00:16] INFO: Telegram notification sent
[2026-03-21 02:00:16] INFO: daily_backup complete
[2026-03-21 08:00:00] INFO: Heartbeat check started
[2026-03-21 08:00:01] INFO: Disk space: 45% (OK)
[2026-03-21 08:00:01] INFO: Memory: 62% (OK)
[2026-03-21 08:00:02] INFO: Nginx: running (OK)
[2026-03-21 08:00:02] INFO: Database: connected (OK)
[2026-03-21 08:00:02] INFO: Heartbeat check complete
[2026-03-21 08:30:00] INFO: Heartbeat check started
[2026-03-21 08:30:01] WARN: Disk space: 82% (WARNING)
[2026-03-21 08:30:01] INFO: Warning notification sent
[2026-03-21 08:30:02] INFO: Heartbeat check complete`}
Best Practices
1. Error Handling
- Every script should have error handling
- Send notifications on failure
- Record detailed error messages
2. Log Management
- Log uniformly to logs directory
- Clean up old logs regularly
- Retain execution history
3. Notification Strategy
| Task Type | Success Notification | Failure Notification |
|---|---|---|
| Backup | ✅ | ✅ Required |
| Health Check | ❌ | ✅ Required |
| Report Generation | ✅ | ✅ |
| Routine Cleanup | ❌ | ✅ |
4. Security
- Use environment variables for sensitive information
- Set script file permissions to 700
- Regularly review Cron tasks
Summary
⏰ Scheduling and Automation allows repetitive tasks to run without human intervention:
- Cron: Scheduled execution for backups, reports, cleanup
- Heartbeat: Status checks for monitoring and health checks
- Error Notifications: Know immediately when problems occur
- Log Records: Complete execution history for troubleshooting