How to save cron job output to file?

Better Stack Team
Updated on October 5, 2023

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.

🔭 Want to get alerted when your Cron doesn’t run correctly?

Go to Better Stack and start monitoring in 5 minutes.

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

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)

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github