Amazon holiday deals See deals →ad

Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

Linux Cron Output: Where It Goes and How to Find It

On Linux, cron usually does not save a job’s output to a file by itself: if a scheduled command writes to standard output or standard error, cron commonly tries to email that output to the crontab’s owner. To keep output in a file, redirect it in the crontab; cron’s own activity is logged separately, often through syslog or the systemd journal.

What Happens to a Cron Job’s Output

A cron job runs a command on a schedule, typically through a shell. If the command prints text, that text goes to standard output (stdout); errors and diagnostics commonly go to standard error (stderr). Traditional cron implementations collect these streams and, when there is output, attempt to send it as email. This is why a command can appear to run successfully while its output is nowhere obvious on disk.

This behavior is a default, not a universal guarantee. Cron versions, system configuration, and mail setup affect whether a message is delivered. A job that produces no output normally has nothing for cron to mail. A job whose output is redirected elsewhere is handled by that redirection instead.

For example, a crontab entry such as 0 2 * * * /home/sam/backup.sh does not specify an output file. If the script prints a completion message or an error, cron may email it rather than append it to a log. Do not confuse that with the logs that record cron’s attempts to start scheduled jobs; those are a separate source of information.

Where Cron Sends Email

When cron mail is enabled and working, output is generally addressed to the account that owns the crontab. The MAILTO variable can be used by many common cron implementations to choose a recipient. For example, placing MAILTO=ops@example.com on its own line in a crontab requests that address for subsequent job output, subject to the host’s mail configuration.

Some implementations also treat an empty MAILTO value as a way to suppress cron’s email. Check the manual for the cron implementation installed on your system before relying on that detail. In particular, an address in the crontab does not itself configure email delivery: cron needs a functioning local mail transport, and the machine must be configured to deliver messages locally or relay them onward.

If there is no mail transfer agent or relay, cron may be unable to deliver the captured output. It might generate a delivery error or leave a message in a local mail queue, depending on the software installed. Thus, seeing no email does not prove that the job produced no output. It is often more reliable to explicitly write important job output to a log file or a monitoring service.

Cron Logs Are Different from Job Output

Cron’s service logs can show that a scheduled command was launched, and sometimes include the user and command. They do not necessarily contain everything the command printed. A line indicating that cron started a job is not the same as the job’s stdout or stderr, so checking the service log alone may not reveal why a script failed.

Where those service messages appear depends on the distribution and logging setup. Common locations and ways to check include:

  • /var/log/syslog on some Debian- and Ubuntu-based systems, often searchable for CRON.
  • /var/log/cron on some Red Hat-family systems and other configurations.
  • /var/log/messages on systems that use that general-purpose log.
  • The systemd journal, where the service may be named cron or crond.

For a systemd host, try journalctl -u cron or journalctl -u crond, depending on the service name. To inspect recent entries, add an option such as --since today. If neither command shows the expected records, check the service name with your distribution’s service tools and inspect the system’s logging configuration. Logs can also be rotated, filtered, or restricted to privileged users.

How to Save Output to a File

Put shell redirections in the command portion of the crontab entry. To append both normal output and errors to one file, use:

0 2 * * * /home/sam/backup.sh >> /home/sam/backup.log 2>&1

The double greater-than operator appends rather than replacing the existing log. The 2>&1 redirects standard error to the same destination as standard output. Its position matters: this ordering sends both streams to the file. By contrast, > /home/sam/backup.log 2>&1 overwrites the file on each run, which can erase useful history.

If you want separate files, use a distinct redirection for each stream, for example >> /home/sam/job.out 2>> /home/sam/job.err. If you want to keep a log but prevent cron from emailing captured output, redirect both streams. Sending them to /dev/null discards them, which is appropriate only when you are confident that losing diagnostic output is acceptable.

Make sure the account running the job can write to the chosen directory. A path under a user’s home directory can be convenient for a personal crontab, while a system service may need a dedicated log directory with appropriate ownership and permissions. Also consider disk usage: an append-only log can grow indefinitely. Use log rotation or another retention policy for a job that runs frequently or produces substantial output.

Crontab Environment and File Paths

A frequent surprise is that a command works in an interactive terminal but behaves differently in cron. Cron supplies a small environment, and its working directory is not necessarily the script’s directory. In particular, relying on a terminal’s expanded PATH or current directory can make a scheduled command fail before it prints the output you expect.

Use full paths for commands and files when practical. For example, write /usr/bin/python3 /home/sam/report.py rather than assuming cron will find python3 or that a relative path such as data/input.csv refers to the intended file. You can define environment variables near the top of a crontab, such as PATH=/usr/local/bin:/usr/bin:/bin, but choose a path appropriate for the system and the job.

Redirection is performed by the command shell, so the directory in a relative log path is resolved from the job’s working directory, not necessarily from the script’s location. An absolute path avoids that ambiguity. The shell used by cron may also differ from the one you use interactively; if a script requires a particular shell, use a suitable shebang and ensure the script is executable, or explicitly invoke the required interpreter.

Diagnosing Missing Output or Email

When a cron job seems silent, work through the stages separately: did cron run it, did the command produce output, and was that output captured or delivered? The following checks can narrow the problem down:

  • Confirm the crontab entry is installed for the intended account. A user’s crontab and a system-wide cron file have different formats; system files commonly include an extra username field.
  • Check cron’s service log or journal for evidence that the schedule was triggered. Remember that this verifies scheduling activity, not necessarily successful completion.
  • Run the command as the same account where possible, using an environment similar to cron’s. Check exit status, permissions, executable paths, and any required environment variables.
  • Temporarily redirect stdout and stderr to a known writable absolute path. Inspect the file after a scheduled run to distinguish command behavior from mail-delivery problems.
  • If you expect email, verify the configured recipient and the host’s local mail transport. Check local mail or the mail queue if those tools are installed and available.

For a more explicit record of success or failure, a wrapper script can print a timestamp, run the command, record its exit status, and then exit with that status. This is useful for diagnosis, but avoid logging credentials, tokens, or sensitive data. Cron logs and job logs may be readable by administrators or other users depending on file permissions.

Cron Output Compared with Systemd Timers

On systems that use systemd, a systemd timer can schedule a service instead of using cron. A service’s stdout and stderr are often collected by the journal according to its configuration, making journalctl a natural place to inspect them. This differs from the traditional cron pattern of capturing output for email, though exact behavior depends on the service settings and how the command is launched.

Neither approach automatically means that every command’s output will be retained forever or be easy for every user to read. With cron, configure redirection or mail intentionally. With a systemd service, check its output and journal settings, retention policy, and permissions. If a machine already has reliable cron monitoring, there is no need to switch solely because a job’s output is missing; first make the existing output destination explicit.

Frequently Asked Questions

Does cron save output to a file by default?
No. Common cron implementations try to email captured output rather than save it to a file. Add shell redirection in the crontab if you need a persistent file.

Where can I see whether cron ran my job?
Check the cron service’s syslog file or systemd journal. Depending on the distribution, search /var/log/syslog, /var/log/cron, or /var/log/messages, or inspect the cron or crond unit with journalctl.

Why did I not receive cron’s email?
The job may have produced no output, mail may be disabled or addressed elsewhere, or the host may lack a working mail transport. Redirect output to a file temporarily to determine whether the command ran and printed anything.

How do I log both errors and normal output?
Append both streams with a command such as your-command >> /var/log/my-job.log 2>&1. Ensure the cron account can write to that path, and arrange log rotation if the file will keep growing.

Does journalctl show my cron command’s output?
Not necessarily. It commonly shows messages from the cron daemon, such as job launches. Traditional cron usually handles command output separately, often by email, unless the job redirects it. Systemd-managed services can have different output handling.

Linux Cron Output: Where It Goes and How to Find It
Scroll to top