ใช้ Cron จัดการทุกอย่างให้อัตโนมัติ
Automate Everything with Cron — easy-to-understand guide based on official docs
ใช้ Cron จัดการทุกอย่างให้อัตโนมัติ
ถ้าคุณเคยอ่าน บทช่วยสอนบอทสรุปข่าวประจำวัน คุณก็รู้พื้นฐานของ cron jobs กันแล้ว แต่นั่นเป็นแค่ส่วนยอดของภูเขาน้ำแข็ง วันนี้เราจะลงลึกกันมากขึ้น — ด้วยรูปแบบการทำงานอัตโนมัติ 5 แบบจากโลกจริง ที่คุณสามารถหยิบไปปรับใช้กับงานของคุณเองได้เลย
ก่อนจะลงรายละเอียด ขอกฎทองไว้ตรงนี้: cron jobs ทำงานในเซสชัน agent ใหม่เอี่ยม โดยไม่มีความทรงจำจากการแชทปัจจุบันของคุณ โปรмпต์ของคุณต้องครบถ้วนในตัวเอง ใส่ทุกอย่างที่ agent ต้องรู้ — คำสั่ง บริบท ความคาดหวัง ให้คิดซะว่าเหมือนการเขียนคำสั่งให้เด็กฝึกงานที่ฉลาดมากๆ แต่เพิ่งเดินเข้าประตูมา
ทางเลือกแบบ Zero-Token (ประหยัดการเรียกใช้ LLM)
ไม่ใช่ว่าทุกงานอัตโนมัติต้องใช้ LLM มีสองตัวเลือกที่ช่วยให้คุณข้ามค่าใช้จ่ายของ token ได้ทั้งหมด:
- Recurring watchdog — ถ้าสคริปต์ของคุณสร้างข้อความที่ต้องการได้อยู่แล้ว (การแจ้งเตือนหน่วยความจำ คำเตือนพื้นที่ดิสก์ สัญญาณ heartbeat) ให้ใช้ cron jobs แบบสคริปต์อย่างเดียว ตัวตั้งเวลาเดียวกัน ไม่ต้องใช้ LLM แค่บอก Hermes ในแชท — เครื่องมือ
cronjobรู้เองว่าเมื่อไหร่ควรเลือกno_agent=Trueและเขียนสคริปต์ให้คุณ - One-shot จากสคริปต์ที่รันอยู่ — สำหรับขั้นตอน CI, post-commit hooks, หรือ deploy scripts ใช้
hermes sendเพื่อส่ง stdout หรือไฟล์ตรงไปยัง Telegram, Discord, Slack ฯลฯ ไม่ต้องสร้าง cron entry เลย
รูปแบบที่ 1: ตัวเฝ้าดูการเปลี่ยนแปลงของเว็บไซต์
เฝ้าดู URL และรับการแจ้งเตือนเฉพาะเมื่อมีอะไรเปลี่ยนแปลงจริงๆ อาวุธลับตรงนี้คือพารามิเตอร์ script — สคริปต์ Python จะรันก่อนทุกครั้ง และ stdout ของมันจะกลายเป็นบริบทให้ agent สคริปต์จัดการงานเชิงกล (การดึงข้อมูล, การเปรียบเทียบ) ส่วน agent จัดการงานเชิงวิเคราะห์ (การเปลี่ยนแปลงนี้น่าสนใจไหม?)
ขั้นแรก สร้างสคริปต์สำหรับเฝ้าดู:
mkdir -p ~/.hermes/scripts
import hashlib, json, os, urllib.request
URL = "https://example.com/pricing"
STATE_FILE = os.path.expanduser("~/.hermes/scripts/.watch-site-state.json")
# ดึงเนื้อหาปัจจุบัน
req = urllib.request.Request(URL, headers={"User-Agent": "Hermes-Monitor/1.0"})
content = urllib.request.urlopen(req, timeout=30).read().decode()
current_hash = hashlib.sha256(content.encode()).hexdigest()
# โหลดสถานะก่อนหน้า
prev_hash = None
if os.path.exists(STATE_FILE):
with open(STATE_FILE) as f:
prev_hash = json.load(f).get("hash")
# บันทึกสถานะปัจจุบัน
with open(STATE_FILE, "w") as f:
json.dump({"hash": current_hash, "url": URL}, f)
# ผลลัพธ์สำหรับ agent
if prev_hash and prev_hash != current_hash:
print(f"CHANGE DETECTED on {URL}")
print(f"Previous hash: {prev_hash}")
print(f"Current hash: {current_hash}")
print(f"\nCurrent content (first 2000 chars):\n{content[:2000]}")
else:
print("NO_CHANGE")
จากนั้นตั้งค่า cron job:
/cron add "every 1h" "If the script output says CHANGE DETECTED, summarize what changed on the page and why it might matter. If it says NO_CHANGE, respond with just [SILENT]." --script ~/.hermes/scripts/watch-site.py --name "Pricing monitor" --deliver telegram
เคล็ดลับ [SILENT]: สำหรับงานเฝ้าดู ให้บอก agent ตอบแค่ [SILENT] เมื่อไม่มีอะไรเปลี่ยนแปลง ระบบส่งข้อความของ cron จะถือว่านี่คือสัญญาณเงียบ — คุณจะได้รับการแจ้งเตือนเฉพาะเมื่อมีอะไรเกิดขึ้นจริงเท่านั้น ไม่มีสแปมในช่วงเวลาที่เงียบสงบ
รูปแบบที่ 2: รายงานประจำสัปดาห์
รวบรวมข้อมูลจากหลายแหล่งเข้าด้วยกันเป็นสรุปที่จัดรูปแบบสวยงาม งานนี้จะรันสัปดาห์ละครั้งและส่งไปยังช่องหลักของคุณ
/cron add "0 9 * * 1" "Generate a weekly report covering:
1. Search the web for the top 5 AI news stories from the past week
2. Search GitHub for trending repositories in the 'machine-learning' topic
3. Check Hacker News for the most discussed AI/ML posts
Format as a clean summary with sections for each source. Include links.
Keep it under 500 words — highlight only what matters." --name "Weekly AI digest" --deliver telegram
จาก CLI:
hermes cron create "0 9 * * 1" \
"Generate a weekly report covering the top AI news, trending ML GitHub repos, and most-discussed HN posts. Format with sections, include links, keep under 500 words." \
--name "Weekly AI digest" \
--deliver telegram
0 9 * * 1 คือนิพจน์ cron มาตรฐาน: 09:00 น. ทุกวันจันทร์
รูปแบบที่ 3: ตัวเฝ้าดู GitHub Repository
เฝ้าดู repository เพื่อติดตาม issues, PRs, หรือ releases ใหม่
/cron add "every 6h" "Check the GitHub repository NousResearch/hermes-agent for:
- New issues opened in the last 6 hours
- New PRs opened or merged in the last 6 hours
- Any new releases
Use the terminal to run gh commands:
gh issue list --repo NousResearch/hermes-agent --state open --json number,title,author,createdAt --limit 10
gh pr list --repo NousResearch/hermes-agent --state all --json number,title,author,createdAt,mergedAt --limit 10
Filter to only items from the last 6 hours. If nothing new, respond with [SILENT].
Otherwise, provide a concise summary of the activity." --name "Repo watcher" --deliver discord
โปรмпต์ที่ครบถ้วนในตัวเองสำคัญมาก สังเกตว่าโปรмпต์มีคำสั่ง gh ที่แน่นอนรวมอยู่ด้วย cron agent ไม่มีประวัติการสนทนาจากการรันครั้งก่อน — ต้องเขียนทุกอย่างให้ชัดเจน (หน่วยความจำถาวรจะโหลดขึ้นมา ดังนั้นการตั้งค่าที่บันทึกไว้ใน MEMORY.md จะยังคงอยู่ แต่ไม่ควรพึ่งพามันสำหรับรายละเอียดที่สำคัญต่องาน)
รูปแบบที่ 4: ไปป์ไลน์การเก็บข้อมูล
เก็บข้อมูลตามช่วงเวลาที่กำหนด บันทึกลงไฟล์ และตรวจจับแนวโน้มเมื่อเวลาผ่านไป รูปแบบนี้ผสมผสานสคริปต์ (สำหรับการเก็บข้อมูล) เข้ากับ agent (สำหรับการวิเคราะห์)
import json, os, urllib.request
from datetime import datetime
DATA_DIR = os.path.expanduser("~/.hermes/data/prices")
os.makedirs(DATA_DIR, exist_ok=True)
# ดึงข้อมูลปัจจุบัน (ตัวอย่าง: ราคาคริปโต)
url = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum&vs_currencies=usd"
data = json.loads(urllib.request.urlopen(url, timeout=30).read())
# เพิ่มลงในไฟล์ประวัติ
entry = {"timestamp": datetime.now().isoformat(), "prices": data}
history_file = os.path.join(DATA_DIR, "history.jsonl")
with open(history_file, "a") as f:
f.write(json.dumps(entry) + "\n")
# ผลลัพธ์สำหรับ agent
print(f"Collected prices at {entry['timestamp']}: {data}")
จากนั้นตั้งค่า cron job ที่รันสคริปต์และให้ agent วิเคราะห์ข้อมูลที่เก็บได้:
/cron add "every 30m" "Run the collection script, then analyze the price history in ~/.hermes/data/prices/history.jsonl. If you notice a significant trend (more than 5% change in the last hour), summarize it. Otherwise respond with [SILENT]." --script ~/.hermes/scripts/collect-prices.py --name "Price tracker" --deliver telegram
รูปแบบที่ 5: วิธี “แค่บอกก็พอ”
ไม่อยากเขียนสคริปต์หรือจำ syntax ของ cron? แค่บรรยายสิ่งที่คุณต้องการเป็นภาษาธรรมดา:
/cron add "every 2h" "Check if there are any new articles about AI regulation on Google News. If there are, summarize the top 3. If nothing new, respond with [SILENT]." --name "AI regulation watch" --deliver telegram
agent จะจัดการที่เหลือเอง — ค้นหาเว็บ กรองผลลัพธ์ และจัดรูปแบบผลลัพธ์
สรุปส่งท้าย
ห้ารูปแบบนี้ครอบคลุมสถานการณ์การทำงานอัตโนมัติที่พบบ่อยที่สุด: การเฝ้าดู การรายงาน การติดตาม repo การเก็บข้อมูล และงานแบบ “แค่บอกก็พอ” ประเด็นสำคัญที่ควรจำ:
- สคริปต์จัดการงานเชิงกล — การดึงข้อมูล การเปรียบเทียบ การเก็บรวบรวม
- agent จัดการงานเชิงวิเคราะห์ — อันนี้น่าสนใจไหม? มีอะไรเปลี่ยนไป? ทำไมถึงสำคัญ?
[SILENT]ช่วยให้คุณไม่โดนสแปม — จะได้รับการแจ้งเตือนเฉพาะเมื่อมีอะไรสำคัญจริงๆ- โปรмпต์ที่ครบถ้วนในตัวเองเป็นสิ่งที่ขาดไม่ได้ — agent เริ่มต้นใหม่ทุกครั้ง
สำหรับเอกสารอ้างอิงฟีเจอร์ทั้งหมด ดูได้ที่ เอกสาร Scheduled Tasks (Cron) ขอให้สนุกกับการทำงานอัตโนมัติ!
📖 เอกสารทางการ
この記事は Hermes Agent のเอกสารทางการに基づいています:เอกสารทางการ › guides/automate-with-cron