🔠Want to get alerted when your Cron doesn’t run correctly?
Go to Better Stack and start monitoring in 5 minutes.
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.
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.
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.
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 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
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.
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.
In this quick tutorial, we will take a look at how to set up a cron job to run at a specific time.
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.
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 usWrite 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.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github