Cron Expression Code Snippets
Enter any cron expression and get production-ready code in your language — Python, Node.js, Go, Ruby, Java, or Bash.
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger
scheduler = BlockingScheduler()
@scheduler.scheduled_job(
CronTrigger.from_crontab("0 9 * * 1")
)
def weekly_report():
"""Runs on schedule: 0 9 * * 1"""
print("Running weekly_report...")
# TODO: Add your job logic here
if __name__ == "__main__":
print("Scheduler started. Job: weekly_report")
print("Schedule: 0 9 * * 1")
scheduler.start()pip install apschedulerLanguage-Specific Tips
Python
APScheduler supports timezone-aware schedules out of the box. Use `timezone='America/New_York'` in the BlockingScheduler constructor.
Node.js
node-cron runs in the same process as your app. For long-running jobs, consider spawning a child process or using a job queue like Bull.
Go
robfig/cron v3 uses standard 5-field cron by default. Add `cron.WithSeconds()` for 6-field expressions with second precision.
Ruby
rufus-scheduler works in-process. For production Rails apps, combine with Sidekiq's cron feature or the `sidekiq-scheduler` gem.
Java
Spring @Scheduled requires `@EnableScheduling` on a @Configuration class. Use `fixedRate` for interval-based schedules without cron.
Bash
Always redirect both stdout and stderr: `>> /var/log/job.log 2>&1`. Use `set -euo pipefail` at the top of every cron script.
FAQ
Which library should I use for Python cron jobs?
APScheduler is the most popular choice for Python cron jobs. It supports crontab syntax, multiple job stores (SQLAlchemy, Redis), and timezone-aware scheduling. For Django projects, consider django-apscheduler or Celery Beat.
How do I run a cron job every 5 minutes in Node.js?
Use node-cron with the expression `*/5 * * * *`. Install with `npm install node-cron`, then call `cron.schedule('*/5 * * * *', () => { ... })`. For distributed systems or job queues, consider BullMQ with repeat options.
Does Spring @Scheduled support cron expressions?
Yes. Use `@Scheduled(cron = '0 9 * * MON')` to run at 9am every Monday. Add `zone = 'America/New_York'` for timezone support. Remember to add `@EnableScheduling` to your application configuration class.
What is the safest way to run cron jobs in Bash?
Always add `set -euo pipefail` to exit on errors, undefined variables, and pipeline failures. Redirect all output: `2>&1`. Use absolute paths for all commands. Add a lock file with `flock` to prevent overlapping runs.
How do I handle timezone in Go cron jobs?
robfig/cron v3 defaults to UTC. Pass a custom location to the constructor: `cron.New(cron.WithLocation(time.LoadLocation('America/New_York')))`. Alternatively, use the CRON_TZ= prefix in your expression.