Cron is a scheduler for recurring tasks on Linux and other Unix-like systems. It is useful for predictable maintenance work: rotating application exports, checking whether a backup completed, rebuilding a local report, or invoking a script at an agreed time. Its simplicity is also its main operational limitation. Cron does not make a script reliable, retry a failed job automatically, or reconstruct a missed run after a host was powered off. Those behaviors must be designed and tested separately.
Administrators get better results by treating a cron entry as a small production deployment rather than a line of mysterious punctuation. The schedule defines when to attempt execution; permissions and environment determine whether the command starts; the script determines whether it finishes safely; and logging establishes what happened. A well-designed job also has a clear owner, a documented failure path, and a strategy for overlapping runs.
Understand the five time fields and where the command belongs
A normal user crontab has five scheduling fields followed by a command. In order, the fields are minute (0–59), hour (0–23), day of month (1–31), month (1–12), and day of week (often 0–7 for Sunday, depending on implementation). An asterisk means every value. Commas select individual values, a hyphen selects a range, and a slash specifies a step through a range. The schedule is evaluated in the time zone configured for the scheduler or job, which must be made explicit when a business deadline matters.
# At 02:15 every day
15 2 * * * /usr/local/bin/daily-report.sh
# Every 15 minutes
*/15 * * * * /usr/local/bin/check-queue.sh
# Weekdays at 18:30
30 18 * * 1-5 /usr/local/bin/closeout.sh
A system crontab such as /etc/crontab or a file in /etc/cron.d/ typically inserts a username before the command. Copying a user-crontab line into that file without adding its run-as user changes how the scheduler interprets the entry. Conversely, adding a username inside a personal crontab makes the username part of the command, not the account under which it runs. Check the relevant local manual instead of assuming every cron implementation accepts identical extended syntax.
The relationship between day-of-month and day-of-week is easy to misread. In conventional Vixie/Cronie-style cron, when both fields have restricted values, a match in either field may cause execution. A line intended for “the first Monday of the month” can therefore run on unwanted dates if it simply specifies the first week and Monday fields together. For complex calendar rules, write and test a date predicate inside the script or choose a scheduler designed for calendar expressions.
Manage crontabs deliberately
Use crontab -l to inspect a user’s schedule and crontab -e to edit it. The latter installs the resulting crontab through the supported utility, which is safer than writing directly into cron’s private spool directory. On supported installations, crontab -T can check a proposed file, but that option is not universal; consult crontab --help or its manual before relying on it. An entry can be syntactically valid and still point to a missing executable or produce incorrect output.
Service names and log locations vary: Debian-like environments often expose cron.service, while systems using Cronie commonly expose crond.service. Verify the service state with systemctl status cron or systemctl status crond as appropriate. A scheduler that is installed but stopped will not run jobs, and a container may deliberately use a different process supervision model. The command to inspect and the scheduling configuration must match the actual operating environment.
Make the execution environment explicit
A cron command normally has fewer environment variables than an interactive login shell. Aliases, shell functions, an expanded PATH, and credentials loaded from interactive startup files may be absent. Therefore an apparently working manual command can fail under cron. Use absolute executable and script paths, set only the required environment variables, and choose a predictable working directory inside the script. Avoid assuming the job starts in the folder where the script resides.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[email protected]
15 2 * * * /usr/local/bin/daily-report.sh >> /var/log/daily-report.log 2>&1
The address above is a placeholder for a configured local mail recipient, not a guarantee that mail delivery exists. Output redirection appends both standard output and standard error to a log that the running account must be allowed to write. Choose log permissions that do not leak database credentials, access tokens, or customer data. Test the script under the same account and restricted environment used by cron; a successful interactive run as root is not meaningful evidence that a less-privileged scheduled task will work.
Shell quoting requires care. The scheduler invokes a command string, and the chosen shell interprets quoting, redirects, pipelines, and expansions. Percent signs also have special handling in some crontab implementations: an unescaped % can split a command and route later content to standard input. If a schedule uses a complex pipeline, move the logic into a version-controlled script that can be linted and unit-tested, leaving the crontab line short and readable.
Schedule reliably across time zones, reboots, and missed runs
Cron usually evaluates the wall clock, so daylight-saving transitions can skip nonexistent local times or repeat a time that occurs twice. Implementations vary in how they compensate for clock changes. Where the task must run exactly once per business period, record the logical period being processed and make the script idempotent, instead of trusting the scheduler alone. For cross-region systems, use a clearly documented time zone and verify the daemon’s handling of job-specific zone settings before deployment.
A host that is down at 02:15 does not normally cause classic cron to replay that day’s entry once it returns. Tools such as anacron and systemd timers can address some missed-run requirements, but they have different semantics and deployment needs. The @reboot shortcut is also implementation-dependent: it runs when the cron daemon starts and is not a reliable substitute for an ordered service that depends on mounted filesystems, DNS, databases, or the network being ready. Use a service manager for startup dependencies and an actual missed-run policy for scheduled workloads.
Prevent overlaps and design failure handling
One of the most dangerous assumptions is that yesterday’s run will be finished before today’s begins. A database job may hang, a backup may be slowed by remote storage, or a network call may wait indefinitely. Without concurrency control, the next scheduled instance can overwrite partial output, contend for a lock, or corrupt shared state. Where a job is not designed for parallel execution, use a lock with a well-defined scope and monitor both skipped and failed starts.
# Example in a user crontab; choose a lock location writable by that user
*/15 * * * * /usr/bin/flock -n /home/report/.report.lock /usr/local/bin/report-job.sh
flock -n avoids waiting for a lock held by another instance, but that means an invocation may be skipped. The script should still use atomic output replacement where appropriate, validate its inputs, and propagate a meaningful exit code. A retry loop that repeats non-idempotent payments or file deletions can be worse than the original failure. State which failures are safe to retry, how an operator receives notification, and when manual recovery is required.
For recurring production work, the operating system’s email notification may not be configured and a quiet command is not proof of success. Record start time, completion status, runtime, processed input version, and an error identifier in a structured or easily searchable log. Expose a success timestamp or metric if missed execution is operationally significant. Monitor the age of the last successful result, not merely whether the cron daemon is running.
Protect the scheduler and the credentials it can reach
A scheduled task runs with the privileges of its configured user. Give it only the file and service permissions required for its work. Store secrets in a protected system facility or a deliberately restricted file, not in the crontab command line or a world-readable script. Ensure scripts and directories cannot be modified by unauthorized users; a writable script invoked by a privileged cron job can turn a small file-permission mistake into a privilege-escalation path.
Log rotation, retention, owner separation, and changes to system crontabs should follow the same review procedures used for other production code. Avoid downloading and executing unverified network scripts directly from a scheduled entry. When new versions are deployed, check that the job’s account still has the necessary permissions and that its notifications still reach a monitored destination. The most important hardening control is often clear ownership: somebody must know why a job exists, whether it still needs to run, and how to disable it safely.
Choose cron or systemd timers based on operational needs
Cron remains a good fit for simple periodic jobs when their execution does not require deep service integration. systemd timers can add features such as service-unit resource controls, journal integration, unit dependencies, and persistent catch-up behavior where explicitly enabled. They do not automatically solve bad scripts, incorrect credentials, or missing monitoring. For application workloads, a job scheduler attached to the application or orchestration platform may be more appropriate than either operating-system timer.
When selecting a scheduler, answer practical questions before adding an entry: Should the job run after the machine was offline? May it overlap? Does it need the network? How will failure be reported? Which account owns its files? Is its clock interpretation stable? Should two servers attempt the same operation, or should one be elected as the worker? These questions are more useful than picking a tool based only on how short its scheduling expression looks.
Scheduling and troubleshooting are also part of current XK0-006 Linux administration skills. In a controlled lab, deploy a nonprivileged crontab, intentionally remove an environment variable, test the resulting log and exit status, introduce an overlapping run, then compare the same workflow with a timer unit. These observations teach the boundary between successful scheduling and successful operations—an essential distinction in real Linux environments.