🔠Want to get alerted when your Cron doesn’t run correctly?
Go to Better Stack and start monitoring in 5 minutes.
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.
Go to Better Stack and start monitoring in 5 minutes.
MAILTO
environmental variable is set in the same way as any other variable
using the =
operator.
As a value of the variable, you can specify either a specific email address or a specific user, or even an empty string in which case no email will be sent.
#mail to an email
MAILTO=email@example.com
#mail to a selected user
MAILTO=someuser
First, open preferred crontab in the select text editor. Then assign a value to
the MAILTO
variable on the line above the select cronjob as shown in the
example below.
MAILTO=email@example.com
* * * * * /bin/backup.sh
This will execute the /bin/backup.sh
shell script every minute. If the script
generates any output during the execution, it will be sent to
email@example.com
.
Make sure that your script doesn't generate output if everything goes as planned as it will send this output regardless of its nature.
You may encounter this error while creating a new crontab or updating an existing one that has a syntax error.
By default, cron checks crontabs for cronjobs every minute. If you want to run a job every n seconds you need to use a simple workaround.
Cron can generate logs, which are very useful in troubleshooting your cron jobs. In this quick tutorial, we will take a look at cron logs – how to find them and how to read them.
Every minute cron goes through all the crontabs and looks for the jobs that should be executed. The exact time of the execution can be specified using the cron syntax. Whenever the parameter matches the current date and time cron job is executed.
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