How to start logging cron job output to syslog on Ubuntu 20.04?
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.
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 executed2>&1
This will forward errors to standard output| /usr/bin/logger -t CRONOUTPUT
This will pipe (forward) standard output to syslog with the nameCRONOUTPUT
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
-
How to list and view all current cron jobs?
Cron is a command-line job scheduler on Unix-like systems. It allows you to run automated tasks in the background and it's especially useful for repetitive jobs.
Questions -
How to prevent duplicate cron jobs from running?
Sometimes you may find that duplicate cronjobs are running at the same time. This may happen when the cronjob takes longer to complete than its execution interval. Here is a simple way to prevent this from happening ever again.
Questions -
How to set up a cron job for a specific time and date?
In this quick tutorial, we will take a look at how to set up a cron job to run at a specific time.
Questions -
How to get cron job errors in email with MAILTO?
One of the neat features of Cron is the ability to send emails when an error occurs during the execution of the cronjob. This can be done using the `MAILTO` environmental variable. When executing cronjob, any output is mailed to the owner of the crontab or to the user or email address specified in the `MAILTO` environment variable in the crontab, if such exists.
Questions
We are hiring.
Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.
Help us in making the internet more reliable.

Help us with developer education and get paid.

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
