How to start logging cron job output to syslog on Ubuntu 20.04?

Better Stack Team
Updated on October 5, 2023

Cron can generate logs, which are very useful in troubleshooting your cron jobs. If yourcron job generates some output, it's collected and sent to you via email. But you may want to send this collected output to the system log instead.

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

Go to Better Stack and start monitoring in 5 minutes.

Why is it helpful you may ask. Well, if all cron job outputs are in one place, it's easier to go through all of them at once as you don't need to switch between directories back and forward. Also, you can display cron outputs using a single command, isn't that cool?

In this quick tutorial, we will take a look at how to redirect output from cron jobs to the main system log.

Where does the cron job output go?

Let's take a look at this example. Imagine that you have the following cron job defined in your crontab:

 
*/10 * * * * /home/user/myscript.sh

This cron job runs every 10 minutes and executes /home/user/myscript.sh shell script. The output of this script will be collected and cron will try to send it to you via email. That means you will only see the following logs in the syslog:

 
Nov 11 12:00:00 server CRON[1011]: (user) CMD (/home/user/myscript.sh)
Nov 11 12:00:00 server CRON[1009]: (CRON) info (No MTA installed, discarding output

Syslog will only show that the /home/user/myscript.sh shell script was executed successfully and that the output of this script was discarded to the missing MTA (Message transfer agent). The output of the script will not be present in the syslog.

How to send cron job output to syslog?

Now, let's redirect the output of the /home/user/myscript.sh shell script to the syslog by changing the crontab to the following:

 
*/10 * * * * /home/user/myscript.sh 2>&1 | /usr/bin/logger -t CRONOUTPUT

Let's break down this code:

  • /10 * * * * This part states that the cron job will run every 10 minutes
  • /home/user/myscript.sh Script that will be executed
  • 2>&1 This will forward errors to standard output
  • | /usr/bin/logger -t CRONOUTPUT This will pipe (forward) standard output to syslog with the name CRONOUTPUT

Now, you can display the output of the cron job in the system log and filter it using the following command:

 
cat /var/log/syslog | grep CRONOUTPUT

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