Hermes Agent cron + scheduled tasks: chạy automation hằng đêm trên VPS

Chia sẻ bài viết

Mục lục
Minh hoạ Hermes Agent cron scheduled tasks chạy đêm trên VPS

VPS chạy Hermes Agent không nên ngồi không - nó nên làm việc khi bạn ngủ. Hermes có cron built-in (không cần tự config crontab Linux): schedule task chạy hằng đêm, hằng giờ, hằng tuần, có log + retry + notify khi fail. Bài này dạy bạn setup pipeline automation hằng đêm trên VPS từ A đến Z.

Hermes cron vs system cron - chọn cái nào

VPS Linux có sẵn cron chạy hàng chục năm rồi - vẫn ổn. Nhưng Hermes có scheduler riêng tích hợp sâu vào agent, hơn hẳn ở 3 điểm:

AspectSystem cronHermes cron
Truy cập skill/toolPhải invoke hermes CLINative, gọi trực tiếp
Context giữa runsStateless, mỗi lần freshCó memory persistent
Log + retryPhải tự codeBuilt-in retry, structured log
Notify khi failMAILTO, hoặc customTelegram/Discord auto
Sửa lịchEdit crontabCommand trong Hermes

Quy tắc đơn giản: task có liên quan đến agent (gọi skill, browser, LLM) -> Hermes cron. Task pure system (backup file, rotate log) -> system cron.

Cron syntax - vẫn là 5 field cổ điển

Hermes giữ syntax cron chuẩn:

* * * * *
| | | | |
| | | | +-- day of week (0-6, Sun=0)
| | | +---- month (1-12)
| | +------ day of month (1-31)
| +-------- hour (0-23)
+---------- minute (0-59)

Một số shortcut tiện:

ShortcutTương đương
@hourly0 * * * *
@daily0 0 * * *
@weekly0 0 * * 0
@monthly0 0 1 * *
@every 15m*/15 * * * *

Add task lịch đầu tiên

Trong Hermes, gõ:

/cron add

Hoặc nói tự nhiên:

You: tạo task chạy mỗi sáng 7h: search Google tin tức "AI agent 2026",
tóm tắt top 5, gửi Telegram chat 123456789

Hermes: [cron.add
  schedule="0 7 * * *"
  name="daily-ai-news"
  action="search 'AI agent 2026' news, summarize top 5, send telegram 123456789"
]
OK, task "daily-ai-news" đã lên lịch. Next run: 2026-05-30 07:00 +07.

List tất cả task:

You: /cron list

Hermes: 3 scheduled tasks:
1. daily-ai-news      @ 0 7 * * *      next: 2026-05-30 07:00
2. weekly-shop-report @ 0 8 * * 1      next: 2026-06-01 08:00
3. hourly-uptime      @ 0 * * * *      next: 2026-05-29 16:00

3 mẫu task thực tế

Task 1: Daily news digest 7h sáng

Mục tiêu: 7h sáng dậy có sẵn bản tin AI/tech tóm tắt trong Telegram.

You: cron @daily lúc 7h:
- search Google "AI agent latest news"
- search x.com "Hermes Agent" trong 24h
- tổng hợp 10 highlight quan trọng
- format markdown
- gửi telegram chat_id mình

Hermes: [cron.add
  schedule="0 7 * * *"
  name="daily-tech-digest"
  action="""
  1. web_search('AI agent latest news', limit=10)
  2. x_search('Hermes Agent', since='24h', limit=20)
  3. merge + dedupe
  4. summarize top 10 với LLM, format markdown
  5. telegram.send_message chat_id=$DEFAULT_CHAT_ID text=summary
  """
]
OK.

Task 2: Weekly scrape report thứ 2

Mỗi thứ 2 lúc 8h, crawl top SP đối thủ, so giá với shop bạn, gửi báo cáo:

You: cron 0 8 * * 1:
- dùng rotate_proxy lấy 1 IP VN
- scrape 50 SP top-seller của shop competitor.shopee.vn/shop/123
- so với data SP của mình từ shopify-product-export.csv
- highlight SP nào competitor rẻ hơn 10% hoặc đắt hơn 10%
- export ra reports/competitor-{date}.csv
- gửi file qua telegram

Hermes: [cron.add name="weekly-competitor-report" schedule="0 8 * * 1" ...]

Task 3: Hourly health check

Check VPS health mỗi giờ, alert khi load > 5 hoặc disk > 80%:

You: cron mỗi giờ:
- chạy shell uptime, df -h, free -m
- parse load average, disk usage, ram free
- nếu load > 5 hoặc disk > 80% hoặc ram free < 500MB
- alert Telegram với chi tiết
- ngược lại không gửi gì (silent OK)

Hermes: [cron.add name="hourly-health" schedule="0 * * * *" ...]
💡 Mẹo: Đặt cron silent-on-success - chỉ alert khi có vấn đề. Không ai muốn 24 notification "OK" mỗi ngày. Tin nhắn càng ít, càng có giá trị.

Log + retry + error handling

Mỗi task chạy ghi log vào ~/.hermes/logs/cron/{task_name}-{timestamp}.log. Format:

[2026-05-29 07:00:01] START daily-tech-digest
[2026-05-29 07:00:02] action: web_search
[2026-05-29 07:00:05] action: x_search ... OK
[2026-05-29 07:00:08] action: summarize ... OK
[2026-05-29 07:00:09] action: telegram.send ... OK
[2026-05-29 07:00:10] DONE in 9.2s

Khi task fail (network drop, LLM timeout, skill error), Hermes retry theo policy:

[cron.retry_policy]
max_attempts = 3
backoff = "exponential"  # 30s, 60s, 120s
on_final_failure = "notify_telegram"

Sau 3 lần fail liên tiếp, agent gửi Telegram alert kèm log trace.

Concurrency - 2 task trùng giờ

Nếu 2 task khác nhau cùng schedule 7h sáng, Hermes mặc định chạy parallel (mỗi task 1 process/thread). Trong môi trường VPS giới hạn RAM, có khi muốn nó tuần tự:

[cron]
max_concurrent = 2  # tối đa 2 task chạy cùng lúc
queue_when_full = true  # task thứ 3 vào queue đợi

Tăng max_concurrent nếu VPS mạnh và task không đụng resource chung (browser, port, file lock).

Dynamic schedule - lịch không phải hard-code

Đôi khi bạn muốn lịch điều chỉnh theo data. Ví dụ: "chạy report vào thứ 6 cuối tháng". Hermes hỗ trợ Python lambda trong schedule:

[cron.task.monthly-report]
type = "dynamic"
should_run = """
from datetime import date, timedelta
today = date.today()
# Friday of last week of month
tomorrow = today + timedelta(days=1)
return today.weekday() == 4 and tomorrow.month != today.month
"""
check_every = "@hourly"
action = "generate monthly report and email"

Cách này linh hoạt hơn cron syntax với rule phức tạp (ngày làm việc, holiday, etc).

Sửa và xoá task

# Pause task tạm thời
/cron pause daily-tech-digest

# Resume
/cron resume daily-tech-digest

# Sửa schedule
/cron edit daily-tech-digest schedule="0 8 * * *"

# Run thử ngay không chờ tới giờ
/cron run-now daily-tech-digest

# Xoá hẳn
/cron delete daily-tech-digest

Run-now là cách test workflow trước khi tin tưởng nó chạy đêm. Luôn run-now 2-3 lần trước khi go-live.

Timezone - lỗi hay gặp nhất

VPS thuê có khi default UTC, không phải giờ VN. Cron "0 7 * * *" thành 7h sáng UTC = 14h VN. Set timezone:

Cách 1 - system level:

sudo timedatectl set-timezone Asia/Ho_Chi_Minh
date  # check
# Fri May 29 15:30:00 +07 2026

Cách 2 - Hermes config:

[cron]
timezone = "Asia/Ho_Chi_Minh"

Cách 2 ưu tiên hơn vì rõ ràng và không phụ thuộc system.

⚠️ Lưu ý: Daylight saving (DST) ở VN không tồn tại nhưng nếu deploy multi-region, cron có thể nhảy 1 giờ vào mùa hè. Luôn set explicit timezone trong config.

Persist qua reboot - systemd

Cron của Hermes chỉ chạy khi Hermes process còn sống. VPS reboot -> Hermes tắt -> cron dừng. Giải pháp: systemd auto-restart.

File /etc/systemd/system/hermes.service (đã đề cập trong bài Telegram bot):

[Service]
Type=simple
User=user
EnvironmentFile=/home/user/.hermes/.env
ExecStart=/home/user/.hermes/venv/bin/hermes --headless --enable-cron
Restart=always
RestartSec=10

Quan trọng: flag --enable-cron để scheduler kích hoạt khi không có terminal interactive.

Theo dõi và debug

Hermes có dashboard log đơn giản:

# Tail log realtime
hermes cron logs --follow

# Xem run history của 1 task
hermes cron history daily-tech-digest --last 10

# Stats theo task
hermes cron stats
# Output:
# task                    runs  success  fail  avg_duration
# daily-tech-digest        30      29      1   9.4s
# weekly-shop-report        4       4      0   45.2s
# hourly-uptime           720     720      0   1.1s

Khi 1 task fail rate > 5%, đào log để hiểu nguyên nhân. Network drop hay LLM rate-limit hay skill bug.

Bandwidth + cost - tránh ngạc nhiên

Task hằng đêm có thể đốt tiền nếu không tính trước:

  • LLM call: 1 task summary = 5k-20k token = 1-5 cent. 30 ngày x 30 task = $30-150/tháng
  • Bandwidth scrape: 100 trang/đêm = ~10MB. 30 ngày = 300MB
  • Proxy: Mỗi cron task qua proxy IPv4 dedicated không tốn thêm (flat fee)
  • Storage log: ~10MB log/tháng, rotate sau 30 ngày

Tính trước rồi xem có nên scale lên VPS lớn không.

VPS đủ cho cron 24/7

Cron tự nó nhẹ. Nặng là task nó chạy. Quy tắc:

Số task hằng ngàyVPS
1-3 task đơn giản (text, search)VPS 50 (4vCPU/4GB) 639k
5-10 task, có browserVPS 80 (6vCPU/6GB) 999k
20+ task, scrape liên tụcVPS lớn hơn + proxy pool

Đọc thêm cách chọn ở bài VPS cho vibe coder. Nếu bạn chưa cài Hermes, bắt đầu từ hướng dẫn cài Hermes + gắn proxy.

Checklist deploy cron production

  • Timezone set Asia/Ho_Chi_Minh trong config
  • Systemd service active với --enable-cron
  • Retry policy max 3, exponential backoff
  • Notify channel (Telegram/Discord) cho fail
  • Log rotation 30 ngày để không full disk
  • Task quan trọng đã /cron run-now test thành công
  • Concurrency limit phù hợp RAM VPS
  • Silent-on-success cho task định kỳ
  • Health-check task ít nhất @hourly để biết khi VPS chết

Bài viết liên quan

VPS chạy Hermes Agent + cron automation hằng đêm

VPS TND Cloud Ceph SSD NVMe, RAM ECC, uptime cao - chuẩn để chạy cron 24/7. VPS 50 (639k) cho task nhẹ, VPS 80 (999k) cho automation có browser + scrape.

Xem bảng giá VPS

2009
15+ năm vận hành liên tục
10+
tập đoàn lớn tin dùng
100+
doanh nghiệp SMB Việt
30 ngày
đổi key lỗi miễn phí
Phần mềm bản quyền chính hãng chúng tôi cung cấp
Bản quyền chính hãng Hóa đơn VAT đầy đủ Đổi key lỗi 30 ngày Vận hành từ 2009 MST 0200994870 Hotline 0225.999.6666