Cron syntax is compact but error-prone, and it is easy to write an expression that does not fire when you expect. This tool takes any standard five-field cron expression and prints the next N times it will trigger, so you can sanity-check a schedule before deploying it. It relies on the croniter library, which understands ranges, steps, and lists exactly as cron does. The expression is validated up front with croniter.is_valid, giving a clear error rather than a confusing traceback for typos. Each upcoming run is shown with its absolute timestamp and a human-readable countdown, which makes it obvious whether "every 15 minutes" really means what you intended. A --utc flag switches between local and UTC time, important when your servers and your laptop live in different time zones. Install the dependency with pip install croniter. This is a fast way to debug deployment schedules and backup jobs.
Cron Expression Next-Run Calculator
Given a cron expression, print the next several times it will fire along with the countdown to each.
16 views
Share
Script
Download .pypython
#!/usr/bin/env python3
"""Calculate the next run times for a cron expression.
Dependency: pip install croniter
"""
import argparse
from datetime import datetime, timezone
from croniter import croniter, CroniterBadCronError
def next_runs(expression, count, base):
"""Return a list of the next `count` datetimes after `base`."""
itr = croniter(expression, base)
return [itr.get_next(datetime) for _ in range(count)]
def main():
parser = argparse.ArgumentParser(
description="Compute upcoming run times for a cron expression.")
parser.add_argument("expression",
help='Cron expression, e.g. "*/15 * * * *".')
parser.add_argument("-n", "--count", type=int, default=5,
help="How many upcoming runs to show (default: 5).")
parser.add_argument("--utc", action="store_true",
help="Use UTC instead of local time.")
args = parser.parse_args()
base = datetime.now(timezone.utc) if args.utc else datetime.now()
# Validate the expression before computing anything.
if not croniter.is_valid(args.expression):
print(f"Invalid cron expression: {args.expression}")
return
try:
runs = next_runs(args.expression, args.count, base)
except CroniterBadCronError as exc:
print(f"Error: {exc}")
return
tz_label = "UTC" if args.utc else "local"
print(f"Cron: {args.expression}")
print(f"From: {base.strftime('%Y-%m-%d %H:%M:%S')} ({tz_label})\n")
for i, run in enumerate(runs, start=1):
delta = run - base
print(f"{i:>2}. {run.strftime('%Y-%m-%d %H:%M:%S')} "
f"(in {str(delta).split('.')[0]})")
if __name__ == "__main__":
main()How to run
Review the script first, then download or copy it and run it in your environment.
You might also like
pythoncross-platformIntermediate
Validate a .env File Against a Schema
Check that a .env file contains all required variables with the correct types before your application starts.
bashlinuxIntermediate
Backup All MySQL Databases with mysqldump
Dumps each MySQL database to its own compressed SQL file.
bashlinuxIntermediate
CPU Load Average Alert
Warns by email when the one-minute load average per CPU core exceeds a configurable ratio.