Cron job examples & use cases
Cron jobs shine when you need simple, time-based automation: run a script every night, send a report every week, or clean up logs every hour. Below are practical cron job examples with ready-to-paste code for Linux crontab, Kubernetes CronJob, and GitHub Actions.
If you're new to cron syntax, start with what is a cron job and crontab syntax & format.
Example 1: Nightly database backup at 02:00
A classic cron job: run a backup script once per night when load is low. This job runs every day at 02:00 server time.
Linux crontab
# 0 2 * * * = every day at 02:00 0 2 * * * /usr/local/bin/backup-db.sh >> /var/log/backup-db.log 2>&1
Kubernetes CronJob
apiVersion: batch/v1
kind: CronJob
metadata:
name: nightly-db-backup
spec:
schedule: "0 2 * * *" # 02:00 every day
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: backup
image: alpine:3.19
command: ["sh", "-lc", "/scripts/backup-db.sh"]
volumeMounts:
- name: scripts
mountPath: /scripts
volumes:
- name: scripts
configMap:
name: backup-scriptsGitHub Actions
name: Nightly database backup
on:
schedule:
- cron: "0 2 * * *" # 02:00 UTC
jobs:
backup:
runs-on: ubuntu-latest
steps:
- name: Run backup script
run: ./scripts/backup-db.shExample 2: Weekday report at 09:00
Many teams send internal reports each morning. This job runs at 09:00 on weekdays (Monday–Friday).
Linux crontab
# 0 9 * * 1-5 = 09:00 Monday–Friday 0 9 * * 1-5 /usr/local/bin/send-daily-report.sh \ >> /var/log/daily-report.log 2>&1
Kubernetes CronJob
apiVersion: batch/v1
kind: CronJob
metadata:
name: weekday-report-0900
spec:
schedule: "0 9 * * 1-5" # 09:00 weekdays
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: report
image: node:20-alpine
command: ["node", "/app/send-report.js"]GitHub Actions
name: Weekday 09:00 report
on:
schedule:
- cron: "0 9 * * 1-5" # 09:00 UTC, Monday–Friday
jobs:
report:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Send report
run: node scripts/send-report.jsExample 3: Hourly log cleanup
Disk usage grows silently until it doesn't. This job removes log files older than 7 days every hour.
Linux crontab
# At minute 5 every hour 5 * * * * find /var/log/my-app -type f -mtime +7 -delete
Kubernetes CronJob
apiVersion: batch/v1
kind: CronJob
metadata:
name: log-cleanup-hourly
spec:
schedule: "5 * * * *" # minute 5 every hour
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: cleanup
image: alpine:3.19
command:
- sh
- -lc
- >
find /var/log/my-app -type f -mtime +7 -deleteExample 4: Health check every 5 minutes
Use a small cron job to probe an internal endpoint and emit metrics or logs if it's down.
Linux crontab
# Every 5 minutes */5 * * * * /usr/local/bin/health-check.sh \ >> /var/log/health-check.log 2>&1
GitHub Actions (public endpoint)
name: Uptime check
on:
schedule:
- cron: "*/5 * * * *" # every 5 minutes
jobs:
ping:
runs-on: ubuntu-latest
steps:
- name: Check status page
run: |
curl -fsS https://status.example.com/health || exit 1Example 5: Monthly billing job on the 1st
Run billing or invoicing logic on the first day of each month at 01:30.
Linux crontab
# 30 1 1 * * = 01:30 on the 1st of every month 30 1 1 * * /usr/local/bin/run-billing.sh \ >> /var/log/billing.log 2>&1
EventBridge / Quartz-style cron
# seconds minutes hours dayOfMonth month dayOfWeek year(optional) # 0 30 1 1 * ? * = 01:30 on the 1st of every month ScheduleExpression: "cron(0 30 1 1 * ? *)"
Design cron jobs that scale with you
Start with a clear use case, then pick a schedule and platform that matches. For each cron job, aim to:
- Use a readable cron expression.
- Keep the command simple and delegate logic to scripts.
- Log output somewhere easy to inspect.
- Monitor exit codes and last successful run.
For more guidance, see cron best practices and cron logging & logs.
Explore common cron expressions
Our expression pages include visual calendars, natural-language explanations, and platform-specific snippets: