How to save cron job output to file?

Better Stack Team
Updated on May 4, 2022

Any cron job can generate output. It may be log or error messages. Regardless of the nature of the output, you may want to save this output to a file. This can be done using the > operator.

Saving cron job output to file

To save the output from the cron job running the shell script to a file, use the following command:

*   *  *   *   *   /bin/backup.sh > /bin/file.txt 2>&1
Copied!

Let's break down this cron job:

  • /bin/backup.sh > /bin/file.txt states that the output from /bin/backup.sh will be redirected and saved in the /bin/file.txt
  • 2>&1 states that the standard error (2>) is redirected to the same file descriptor that is pointed by standard output (&1)
Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →

Reliability is the
ultimate feature

Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.

Explore Better Stack