Save Cron output to a file

  • 1 Min. Read.

To save the output of a Cron job to a file, you can use the following command:

This script will execute /task.sh every hour, and append the output to /var/log/task.log.

What does 2>&1 mean?

  • 2 refers to the second file descriptor of the process, i.e. stderr.
  • > means redirect.
  • &1 means the target of the redirection should be the same location as the first file descriptor, i.e. stdout.

In other words, both standard output and errors will be written to the log file specified.

Add timestamps to every line

It may come in handy to have a reference at what time each line was appended to the log file. Create a file called /date.sh with the following contents:

And in crontab, modify your command like this:

This will add a nice timestamp before every line that gets written to the log file.

Was this article helpful?

Comments 0
There are no comments at the moment.
A concept by Wonderlus

Warning: Parameter 1 to W3_Plugin_TotalCache::ob_callback() expected to be a reference, value given in /mnt/home_bingo/codecaptain/codecaptain.io/public/wp-includes/functions.php on line 3583