EXAMTOPICS / TECHNOLOGY INSIGHTS

Linux cron Jobs: Scheduling and Troubleshooting

Cron is a traditional Linux and Unix job scheduler that runs commands at specified calendar times without an interactive user. Administrators use it for log rotation scripts, recurring reports, data exports, and maintenance work. It is deliberately simple: a crontab declares a schedule and a command, and the cron daemon evaluates those entries at regular intervals. That simplicity is useful only when the job’s environment, privileges, error reporting, and retry behavior are understood.

A scheduled job is not automatically reliable just because a line has been added to a crontab. You need to know which account executes it, which shell and environment it receives, what happens when the machine is off, and how success or failure will be detected. Those operational questions are as important as the five scheduling fields.

Read the five-field crontab expression

A standard per-user cron entry contains five timing fields followed by the command:

minute hour day-of-month month day-of-week command

Minutes range from 0 to 59, hours from 0 to 23, and months from 1 to 12. The day-of-month field covers dates 1 through 31, and the day-of-week field is typically 0–7, with 0 and 7 representing Sunday in common cron implementations. An asterisk represents the full range. Commas select a list, hyphens select an inclusive range, and slash notation selects a step within the relevant range.

For example, 15 2 * * * /usr/local/bin/nightly-report means 02:15 every day, in the time zone used by that crontab and daemon. 0 9 * * 1-5 runs on weekdays at 09:00. */15 * * * * runs at minute 0, 15, 30, and 45 of each hour; it does not mean “15 minutes after the previous run finished.” Cron matches the clock, not elapsed time since task completion.

An easy trap concerns the two day fields. In common Vixie/Cronie-style implementations, if both day-of-month and day-of-week are restricted, a match on either field can trigger the job. For example, 0 8 1 * 1 may run on the first day of the month and each Monday, rather than only when the first is Monday. Read the manual for the daemon on your system before using a complex combination.

Install and inspect jobs with the crontab command

Use crontab -l to list the current user’s scheduled entries and crontab -e to edit them. To choose nano for this edit, EDITOR=nano crontab -e works on systems where nano is installed and the command honors that environment variable. When editing in GNU nano, confirm that the crontab is saved and installed; leaving the editor without writing the changes will not schedule the intended job.

Use crontab -u account -l only when authorized to inspect another account’s schedule. User crontabs and system crontabs have different layouts. A per-user crontab has five time fields followed directly by the command; /etc/crontab and files under /etc/cron.d commonly add a username field before the command. Copying a system entry verbatim into a per-user crontab can accidentally execute the username as though it were a command.

Before scheduling anything, run the command manually under the same service account and from an intentionally minimal environment. If a command works in an interactive root shell but fails for a nonprivileged service account, the cron expression is not the problem. Verify execution permissions and ownership instead of adding sudo reflexively.

Use explicit paths and a controlled environment

Cron jobs typically have a small environment. Interactive shell aliases, profile scripts, version managers, and graphical desktop variables might not be loaded. Write the script so it does not depend on the current working directory, shell history, or aliases. Use absolute paths for important executables, input files, and output files, and declare an appropriate PATH only when the actual system requires it.

For a nightly export, a robust shell script might begin by setting a known working directory, validating the input mount, and using an output filename that includes an unambiguous date. It should exit nonzero on failure and send diagnostic information to a controlled log location. Avoid storing database passwords directly in world-readable crontab text. Use appropriate secret-management facilities and file permissions, with access restricted to the scheduled account.

Script files need correct permissions and executable formats. The distinction between Linux file owners, groups, and permissions is especially relevant when a scheduled script can be read manually but cannot be executed, or when it fails to create output in a protected directory.

Log results and stop overlapping runs

Cron itself starts a command at the scheduled moment; it does not guarantee that the work completed successfully. A basic entry can redirect standard output and errors to a writable log:

15 2 * * * /usr/local/bin/nightly-report >> /var/log/nightly-report.log 2>&1

This is only appropriate if the user account can write to that path and log retention is controlled. On many systems, a per-user log directory is safer than /var/log. An application may also send mail for job output if local mail delivery and MAILTO are configured. Confirm the monitoring path actually works rather than assuming mailed errors reach an administrator.

Overlapping runs are dangerous for backups, exports, package operations, and cleanup. If one job lasts more than the interval between starts, two copies may modify the same files or database state. Where available, a lock such as flock can prevent simultaneous execution; otherwise implement application-level idempotency or a durable lock. Test what happens when a process crashes while holding that lock, and make the skipped-run behavior visible in monitoring.

A job that deletes files needs particular care. Never schedule broad globs or recursive deletion under elevated privileges without first validating the target path and enumerating the files that would be affected. Schedule dry-run checks, keep recoverable backups, and require review before automating a destructive operation.

Understand time zones, missed runs, and daylight saving time

Cron’s calendar interpretation depends on daemon implementation, local time zone, and sometimes CRON_TZ. A machine that is powered off during a scheduled minute will normally miss a traditional cron run; it is not automatically replayed later. A clock change during daylight-saving transitions can skip or repeat a local time, and a job configured for 02:30 may behave differently around those transitions.

For workloads where missing one invocation is unacceptable, provide catch-up logic inside the application, use a durable scheduler, or consider a systemd timer on a system that supports it. A systemd.timer with OnCalendar= and carefully configured Persistent=true can trigger a missed calendar run after a period of inactivity, subject to its service design. These mechanisms are not interchangeable with the portable five-field cron format, and timer definitions have their own testing requirements.

Do not schedule critical work solely from a human-readable local time without confirming its required time zone. A billing job intended to run once for each UTC date should use a stable date key and application-level duplicate protection; cron alone cannot enforce exactly-once financial processing.

Debug a job that never seems to run

Check that the cron service is installed and running under the distribution’s service manager. Check crontab -l for the intended account, inspect the five fields, and confirm that the script path and executable exist. Inspect the daemon’s logs using the distribution’s journal or system log facilities, then inspect application output separately. “Cron invoked the command” is different from “the command succeeded.”

Make a short-lived diagnostic job that writes a timestamp to a user-writable file, allow one invocation, and remove the test. If the timestamp appears but the real script fails, focus on environment, ownership, missing credentials, or unexpected current directories. If neither runs, inspect the scheduler, account restrictions, time zone, and syntax. Avoid putting secrets in temporary diagnostic logs.

The commonly used crontab manual describes implementation-specific details such as percent-sign parsing, environment variables, and the special interaction between the two day fields. The percent sign can be particularly surprising: in common cron implementations an unescaped % in a command is interpreted specially, so a command such as date +%F may not behave as expected when pasted directly into a crontab. Put complex commands in a script or escape the character as the implementation requires.

Use a small, testable deployment checklist

A safe first deployment can use a report that only reads data. Save a script under a service account’s directory, make it runnable, and invoke it manually as that same account. Confirm the output and exit code before adding a once-per-day crontab entry. Inspect the installed entry with crontab -l, wait for the next scheduled window, and verify that output has the expected timestamp. Check that it does not silently fail when the host reboots or the output destination is temporarily unavailable.

Do not assume the first successful run validates a schedule indefinitely. A backup can pass today and fail when a destination fills up; a report can execute successfully but produce an empty or corrupt file. Use independent validation such as a backup restore test, a minimum expected row count, or an alert when a completion marker is missing. Rotate logs so that the diagnostic mechanism does not exhaust the disk it was intended to maintain.

Decide when cron is the right scheduler

Cron excels at straightforward, recurring tasks on a machine that is reliably available. It does not provide a dependency graph, transactional retries, job ownership dashboards, or distributed exactly-once execution. For maintenance that must run after a previous job succeeds, or for work that requires secrets, queues, and detailed audit records, a service manager or workflow scheduler may be more suitable.

Good cron automation begins with a testable script and a clear success condition. Only then should a schedule be added. Keep permissions narrow, use explicit paths, monitor outcomes, prevent overlapping runs, and rehearse failure recovery. Those practices make the difference between a task that starts on time and an automated process an operator can actually trust.