ตัวแยกวิเคราะห์นิพจน์ Cron
วิเคราะห์นิพจน์ cron ให้เป็นคำอธิบายที่อ่านได้ง่าย และแสดงตัวอย่างเวลาทำงานถัดไป
นิพจน์ cron
minute hour day(month) month day(week)
คำอธิบายที่อ่านได้ง่าย
At 9:00 AM, Mon, Tue, Wed, Thu, Fri
รายละเอียดของแต่ละช่อง
Minute
0
0–59
Hour
9
0–23
Day (month)
*
1–31
Month
*
1–12
Day (week)
1-5
0–6
10 ครั้งทำงานที่กำหนดไว้ถัดไป
การวิเคราะห์นิพจน์ Cron คืออะไร?
นิพจน์ cron คือสตริงที่ประกอบด้วยห้าช่องคั่นด้วยช่องว่าง ซึ่งกำหนดตารางงานที่เกิดซ้ำ รูปแบบนี้มีต้นกำเนิดจาก Unix cron daemon ที่ Ken Thompson เขียนขึ้นสำหรับ Version 7 Unix ในปี 1979 แต่ละช่องแทนหน่วยเวลา ได้แก่ นาที ชั่วโมง วันในเดือน เดือน และวันในสัปดาห์ การวิเคราะห์นิพจน์ cron หมายถึงการแปลงสัญกรณ์กระชับนี้ให้เป็นคำอธิบายที่อ่านได้ง่าย และรายการเวลาดำเนินการที่จะเกิดขึ้นจริง
รูปแบบห้าช่องมาตรฐานใช้ใน crontab บน Linux และ macOS ในแพลตฟอร์ม CI/CD อย่าง GitHub Actions และ GitLab CI ในระบบกำหนดตารางงานบนคลาวด์ของ AWS (EventBridge) Google Cloud Scheduler และ Azure Functions รวมถึงในไลบรารีจัดตารางงานของภาษาโปรแกรมสำคัญทุกภาษา บางระบบขยายรูปแบบเป็นหกหรือเจ็ดช่องโดยเพิ่มช่องวินาทีหรือช่องปี แต่เค้าโครงห้าช่องที่กำหนดโดย POSIX ยังคงเป็นพื้นฐานร่วมกันของทุกการนำไปใช้งาน
การวิเคราะห์นิพจน์ cron ด้วยตนเองมีแนวโน้มเกิดข้อผิดพลาดสูง การทำงานร่วมกันของแต่ละช่องสร้างตารางงานที่ไม่ชัดเจน เช่น 0 9 1-7 * 1 ไม่ได้หมายถึง 'ทุกวันจันทร์ในสัปดาห์แรก' แต่หมายถึง 'วันที่ 1 ถึง 7 ของทุกเดือน หรือ วันจันทร์ทุกวัน' เครื่องมือวิเคราะห์ cron ขจัดความคลุมเครือนี้โดยการขยายแต่ละช่อง ใช้กฎการรวมกัน และแสดงเวลาจริงที่งานจะทำงาน
เหตุใดจึงควรใช้ตัววิเคราะห์ Cron นี้?
การอ่านนิพจน์ cron เช่น 30 */6 1,15 * * จากไฟล์คอนฟิกและทราบว่ามันทำงานเมื่อใดนั้นต้องคำนวณจากห้าช่องในใจ เครื่องมือวิเคราะห์นี้ทำงานนั้นได้ทันที
กรณีการใช้งานตัววิเคราะห์ Cron
เอกสารอ้างอิงไวยากรณ์นิพจน์ Cron
นิพจน์ cron มาตรฐานมีห้าช่องคั่นด้วยช่องว่าง แต่ละช่องรับจำนวนเต็ม wildcards, ranges, lists และ step values ตารางด้านล่างแสดงช่วงที่อนุญาตและตัวดำเนินการสำหรับแต่ละช่อง
| ช่อง | ช่วง | ตัวดำเนินการ | คำอธิบาย |
|---|---|---|---|
| Minute | 0–59 | * , - / | Minute within the hour |
| Hour | 0–23 | * , - / | Hour of the day (24-hour) |
| Day (month) | 1–31 | * , - / | Day of the month |
| Month | 1–12 | * , - / | Month of the year (or JAN–DEC) |
| Day (week) | 0–6 | * , - / | Day of the week (0 = Sunday, or SUN–SAT) |
อักขระพิเศษสี่ตัวควบคุมวิธีที่ค่าถูกจับคู่ภายในแต่ละช่อง:
| อักขระ | ชื่อ | พฤติกรรม |
|---|---|---|
| * | Wildcard | Matches every possible value in the field. * in the minute field means "every minute." |
| , | List | Separates individual values. 1,15 in the day field means "the 1st and 15th." |
| - | Range | Defines an inclusive range. 1-5 in the day-of-week field means "Monday through Friday." |
| / | Step | Defines an interval. */10 in the minute field means "every 10 minutes." 5/15 means "5, 20, 35, 50." |
นิพจน์ cron ทั่วไปที่ครอบคลุมความต้องการการกำหนดตารางงานส่วนใหญ่:
| นิพจน์ | ตารางงาน |
|---|---|
| * * * * * | Every minute |
| 0 * * * * | Every hour (at minute 0) |
| */15 * * * * | Every 15 minutes |
| 0 9 * * * | Every day at 9:00 AM |
| 0 9 * * 1-5 | Weekdays at 9:00 AM |
| 30 2 * * 0 | Every Sunday at 2:30 AM |
| 0 0 1 * * | First day of every month at midnight |
| 0 0 * * 1 | Every Monday at midnight |
| 0 0 1 1 * | January 1st at midnight (yearly) |
| 0 */6 * * * | Every 6 hours |
| 5,35 * * * * | At minute 5 and 35 of every hour |
| 0 9-17 * * 1-5 | Every hour from 9 AM to 5 PM, weekdays |
ตัวอย่างโค้ด
วิธีวิเคราะห์นิพจน์ cron และคำนวณเวลาทำงานถัดไปในภาษาโปรแกรมยอดนิยม:
import cronstrue from 'cronstrue';
// Parse cron to human-readable text
cronstrue.toString('0 9 * * 1-5');
// → "At 09:00 AM, Monday through Friday"
cronstrue.toString('*/15 * * * *');
// → "Every 15 minutes"
// Validate with cron-parser and get next run times
import { parseExpression } from 'cron-parser';
const interval = parseExpression('30 2 * * 0');
console.log(interval.next().toISOString());
// → next Sunday at 02:30 UTC
// Iterate over the next 5 runs
for (let i = 0; i < 5; i++) {
console.log(interval.next().toString());
}from crontab import CronTab
from croniter import croniter
from datetime import datetime
# Parse and describe a cron expression
cron = CronTab('0 9 * * 1-5')
print(cron.next(default_utc=True))
# → seconds until next run
# Get the next 5 run times with croniter
base = datetime.now()
cron_iter = croniter('0 9 * * 1-5', base)
for _ in range(5):
print(cron_iter.get_next(datetime))
# → next 5 weekday 09:00 timestamps
# Check if a specific time matches
print(croniter.match('*/15 * * * *', datetime(2026, 3, 25, 10, 30)))
# → True (minute 30 is divisible by 15)package main
import (
"fmt"
"time"
"github.com/robfig/cron/v3"
)
func main() {
// Parse a standard 5-field cron expression
parser := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
schedule, err := parser.Parse("0 9 * * 1-5")
if err != nil {
panic(err)
}
// Calculate the next 5 run times
now := time.Now()
for i := 0; i < 5; i++ {
now = schedule.Next(now)
fmt.Println(now)
}
// → next 5 weekday 09:00 timestamps
}# List current user's cron jobs crontab -l # Edit cron jobs interactively crontab -e # Add a job: run backup.sh every day at 2:30 AM # (append to crontab via pipe) (crontab -l 2>/dev/null; echo "30 2 * * * /home/user/backup.sh") | crontab - # Check syntax with a dry-run parse (requires cronie or busybox) # The system will reject invalid expressions when saving # View cron logs on systemd-based Linux journalctl -u cron --since "1 hour ago"