What Is a Cron Expression
A Cron expression is a string format for configuring scheduled task execution, widely used in Linux crontab, CI/CD pipelines, Node.js task schedulers, and distributed job systems.
A standard Cron expression consists of 5 time fields:
┌───────────── Minute (0-59)
│ ┌───────────── Hour (0-23)
│ │ ┌───────────── Day of Month (1-31)
│ │ │ ┌───────────── Month (1-12)
│ │ │ │ ┌───────────── Day of Week (0-7, 0 and 7 = Sunday)
│ │ │ │ │
* * * * *
Why You Need a Cron Expression Parser
- Scheduled tasks: Server backups, data sync, report generation
- CI/CD scheduling: Timed builds and deployments
- Monitoring: Periodic health checks
- Data processing: ETL jobs, log rotation
- Understanding complex expressions: Visualize next execution times
Cron Field Syntax
Special Characters
| Character | Meaning | Example |
|-----------|---------|---------|
| * | Any value | * * * * * = every minute |
| , | List | 1,3,5 * * * * = minutes 1, 3, 5 |
| - | Range | 1-5 * * * * = minutes 1 through 5 |
| / | Step | */15 * * * * = every 15 minutes |
Common Cron Expressions
| Expression | Meaning |
|------------|---------|
| * * * * * | Every minute |
| 0 * * * * | Every hour on the hour |
| 0 0 * * * | Daily at midnight |
| 0 9 * * 1-5 | Weekdays at 9:00 AM |
| 0 0 1 * * | First of every month at midnight |
| 0 0 * * 0 | Every Sunday at midnight |
| */5 * * * * | Every 5 minutes |
| 0 */2 * * * | Every 2 hours |
| 30 8 * * 1 | Monday at 8:30 AM |
| 0 0 1,15 * * | 1st and 15th of every month |
How to Use an Online Cron Parser
Using DevToolkit Pro's Cron Expression Parser:
- Enter a Cron expression (e.g.,
*/15 * * * *) - The tool parses each field's meaning
- Shows a human-readable description (e.g., "Every 15 minutes")
- Lists the next 5-10 execution times
- Syntax error detection included
Cron in Code
Node.js
const cron = require('node-cron');
// Every 5 minutes
cron.schedule('*/5 * * * *', () => {
console.log('Running health check');
});
// Daily at 2:00 AM
cron.schedule('0 2 * * *', () => {
console.log('Running data backup');
});
Python
from crontab import CronTab
# Parse a Cron expression
cron = CronTab('* * * * *')
print(cron.description()) # "Every minute"
# Get next execution time
from datetime import datetime
next_run = cron.get_next(datetime)
Go
import "github.com/robfig/cron/v3"
c := cron.New()
// Every 5 minutes
c.AddFunc("*/5 * * * *", func() {
fmt.Println("Running health check")
})
c.Start()
Important Notes
Timezone
Cron expressions default to the server's local timezone. In distributed systems, use UTC consistently:
# UTC time
0 2 * * * # Daily at UTC 02:00
Sunday Representation
Different systems represent Sunday differently:
0= Sunday (most systems)7= Sunday (some systems)1= Monday
Minimum Granularity
Standard Cron's minimum granularity is minutes. For second-level scheduling, use extended Cron syntax (e.g., */5 * * * * * — 6 fields).
FAQ
What's the Relationship Between Cron Expressions and crontab?
A Cron expression is the string representation of a scheduling rule. crontab is the file/command in Linux for configuring scheduled tasks. You write Cron expressions into crontab to configure jobs.
Are */15 * * * * and 0,15,30,45 * * * * the Same?
They produce the same result — both execute every 15 minutes. */15 is shorthand step syntax.
How Do I Schedule Monday Through Friday?
Use * * * * 1-5. In the day-of-week field, 1=Monday, 5=Friday, 0 or 7=Sunday.
This article is brought to you by DevToolkit Pro. More developer tools at the homepage.
相关文章
Online Word Counter: Count Words, Characters, Lines Instantly
Learn how to use an online word counter to count words, characters, lines, and paragraphs. Understand Chinese vs English counting differences and use cases in writing and development.
UUID Generator: Complete Guide to Unique Identifiers (UUID, NanoID, ULID)
Understand UUID v4, NanoID, and ULID fundamentals. Learn how to use an online UUID generator, and discover best practices for databases, distributed systems, and API tracking.
Online User-Agent Parser: Identify Browser and Device Information
Learn how to parse User-Agent strings. Understand browser, operating system, and device type identification methods, and applications in data analytics and compatibility testing.