Reliable scheduled work is less about writing a cron expression than about controlling the conditions around it. This guide explains how to schedule jobs on Linux and Kubernetes, make their output observable, prevent overlapping runs, handle time zones and daylight-saving changes, and recover when a task is late, duplicated, or silently fails.
Overview
A scheduled job has at least four separate responsibilities: deciding when it starts, defining what environment it runs in, recording what happened, and determining what to do after failure. A five-field cron expression covers only the first responsibility.
Linux cron remains useful for straightforward host-level tasks such as backups, report generation, cache cleanup, and certificate checks. For jobs that need dependency management, explicit service supervision, stronger logging, or more controlled retry behavior, a systemd timer may be a better fit. Kubernetes CronJobs are appropriate when the work should run as a container in the cluster, while a managed cloud scheduler can be preferable when the target is an external HTTP endpoint, queue, function, or cloud-native workflow.
Before choosing an implementation, document the job's owner, expected duration, acceptable delay, time zone, maximum retry behavior, and whether two instances may run at once. These details turn a recurring command into an operationally manageable developer tool.
What to track
Cron expression syntax
Traditional Linux cron uses five fields. The exact behavior of special extensions can vary between cron implementations, so test expressions on the target operating system rather than relying only on an online cron expression builder.
| Field | Allowed values | Example |
|---|---|---|
| Minute | 0–59 | 15 |
| Hour | 0–23 | 2 |
| Day of month | 1–31 | 1 |
| Month | 1–12, or names supported by the implementation | 1 |
| Day of week | 0–7 on implementations where Sunday may be 0 or 7 | 1 |
Common operators are * for every value, a comma for a list, a hyphen for a range, and a slash for a step. For example:
*/5 * * * *runs every five minutes.0 2 * * *runs at 02:00 every day.30 9 * * 1-5runs at 09:30 on weekdays.0 0 1,15 * *runs at midnight on the first and fifteenth days of the month.
Be especially careful with schedules that specify both day-of-month and day-of-week. Implementations commonly treat those fields as an OR condition when both are restricted, but this is a detail worth verifying in the local cron documentation.
Environment, paths, and permissions
Cron jobs often run with a smaller environment than an interactive shell. A command that works in a terminal may fail because PATH, the working directory, credentials, locale, or shell options are different. Use absolute paths, set required variables explicitly, and change to the intended directory before running application commands.
#!/usr/bin/env bash
set -Eeuo pipefail
cd /srv/example-app
/usr/bin/python3 /srv/example-app/scripts/export.py
Keep secrets out of crontabs and command-line arguments where possible. Use an appropriate secret store or protected environment file, and review scheduled jobs alongside your secrets rotation runbook. The job account should have only the permissions it needs.
Logging, exit codes, and alerts
A scheduled task should leave evidence of both success and failure. Capture standard output and standard error, include a start and finish message, and return a nonzero exit code when the work is incomplete. A basic host-level entry might look like this:
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
15 2 * * * /usr/bin/flock -n /run/lock/example-export.lock \
/usr/local/bin/run-example-export.sh \
>> /var/log/example-export.log 2>&1
Log rotation matters as much as log creation. Include a job identifier, schedule time, actual start time, duration, records or objects processed, and an outcome such as success, failed, or skipped_locked. Send alerts based on a missed expected run or repeated failure rather than alerting on every transient message. If the task supports metrics, track last successful completion, duration, failure count, and age of the latest successful result. These signals fit naturally into broader SLO and error budget reviews.
Overlap, timeout, and retry behavior
A schedule does not guarantee that the previous run has finished. Use a lock for work that must not overlap. On Linux, flock is a simple option; the -n flag causes a second invocation to exit instead of waiting indefinitely. Decide whether a skipped run is acceptable or should generate an alert.
Set an explicit timeout around work that can hang. A wrapper can use the system's timeout command:
#!/usr/bin/env bash
set -Eeuo pipefail
exec /usr/bin/timeout --signal=TERM --kill-after=30s 20m \
/usr/local/bin/export-data
Retries should be limited and safe. Retrying a read-only report is different from retrying a payment, migration, or destructive cleanup. Make operations idempotent where possible, use an idempotency key for externally visible actions, and record an attempt number. A retry that creates duplicate side effects is not a recovery strategy.
Linux cron, systemd timers, and Kubernetes CronJobs
Use Linux cron for small, stable host tasks with modest operational requirements. Systemd timers provide a more explicit service model: the timer activates a separate service unit, and service logs can be inspected with the journal. They also support calendar expressions and elapsed-time schedules, but their behavior and available options depend on the systemd version.
Kubernetes CronJobs create Jobs according to a schedule. A practical manifest should specify the time zone when the cluster version supports the timeZone field, prevent or limit overlap, retain enough history for diagnosis, and define a deadline and retry policy:
apiVersion: batch/v1
kind: CronJob
metadata:
name: nightly-export
spec:
schedule: "0 2 * * *"
timeZone: "Etc/UTC"
concurrencyPolicy: Forbid
startingDeadlineSeconds: 1800
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 5
jobTemplate:
spec:
backoffLimit: 2
activeDeadlineSeconds: 1200
template:
spec:
restartPolicy: Never
containers:
- name: exporter
image: registry.example.com/exporter:stable
command: ["/app/export"]
Validate these fields against the Kubernetes version you operate. Forbid prevents a new Job from starting while a prior one is active; it does not make the task idempotent. startingDeadlineSeconds addresses a late start, while activeDeadlineSeconds limits an active Job. Review pod logs and Job status, and check resource requests and limits so a scheduled workload is not repeatedly evicted or killed under pressure. The related Kubernetes resource sizing guide provides a useful review framework.
Cadence and checkpoints
Time zones deserve an explicit decision. UTC is usually the least surprising choice for infrastructure tasks, while business processes may need a named regional time zone. Avoid relying on the host's implicit local time. Daylight-saving transitions can create a nonexistent local time in spring or a repeated local time in autumn; a job may therefore run late, run twice, or require implementation-specific handling. Test the schedule around the next time-zone transition and document the expected result.
Check recurring jobs at three levels:
- Before deployment: test the expression, command, permissions, credentials, timeout, lock, and failure path. Run the command as the same user or service account used by the scheduler.
- After deployment: confirm the scheduler accepted the definition, observe at least one successful run, and inspect logs from both success and failure cases. For Kubernetes, use commands such as
kubectl get cronjob,kubectl get jobs,kubectl describe cronjob <name>, andkubectl logs job/<job-name>. - During routine operations: review last-success time, duration, retries, skipped overlaps, output freshness, and resource consumption. Compare actual start times with the declared time zone.
For host jobs, useful inspection commands include crontab -l, systemctl status cron or the distribution's equivalent service name, journalctl, and a direct test using the scheduler's service account. For systemd timers, inspect both the timer and its service with systemctl list-timers, systemctl status example.service, and journalctl -u example.service.
How to interpret changes
A missed run does not always mean the expression is wrong. First separate scheduling failure from application failure. Confirm that the scheduler was running, the definition was loaded, the target host or cluster was available, and the job was not blocked by a lock or deadline. Then inspect the command's exit code and logs.
A duplicate run can result from daylight-saving behavior, overlapping invocations, a controller restart, a manual rerun, or an operation that was retried after an uncertain network response. Treat every externally visible operation as potentially repeated. Use unique job or execution identifiers and make writes idempotent.
Longer duration is an early warning even when the job still succeeds. Compare runtime with the overlap threshold, inspect CPU and memory pressure, and check whether input volume has changed. In Kubernetes, review pod events and resource usage; a job that is repeatedly OOMKilled or pending may require revised resource requests and limits rather than a scheduling change.
When a job fails, preserve the failed run's logs before deleting resources, identify whether the error is transient or deterministic, and avoid increasing retries without understanding the side effect. If the task affects a service, connect its failure to the relevant monitoring and incident response process. A useful operational record includes the intended schedule, observed behavior, impact, mitigation, and a follow-up owner.
When to revisit
Review scheduled jobs monthly or quarterly, depending on their impact. Revisit immediately after a time-zone or daylight-saving change, host migration, Kubernetes upgrade, container image change, credential rotation, database migration, or change in input volume. Also review a job after any missed, duplicate, unusually slow, or manually recovered run.
Use this compact checklist during each review:
- Is the schedule expressed in the intended time zone and still understood by the team?
- Does the command run with explicit paths, permissions, credentials, and working-directory assumptions?
- Are output, error logs, exit codes, duration, and last-success time visible?
- Can two runs overlap, and is the lock or Kubernetes concurrency policy appropriate?
- Are timeout, retry, backoff, and idempotency behavior safe for the operation?
- Are failed runs retained long enough to investigate without filling storage?
- Do resource usage and output freshness still match the job's operational importance?
Finally, test recovery rather than only success. Stop a dependency, force a timeout in a safe environment, run two copies at once, and simulate a late start. Record the expected alert and the recovery command. For Kubernetes workloads, include scheduled jobs in cluster upgrade planning using the Kubernetes upgrade checklist. A scheduler is reliable when its normal path is predictable and its failure path is documented, observable, and safe to repeat.