An automated script can save time when doing repetitive tasks.

Cron job comes as a boon while executing tasks at an interval or at a specific time.

However, crontab not sending email is a common error that happens due to incorrect settings in the crontab file.

At Bobcares, we often get requests to fix crontab not sending email as part of our Server Management Services.

Today, let’s get into the details and see how our Support Engineers fix crontab not sending an email.

 

Why CRON Jobs?

Many clients want to perform regular tasks on their servers such as backups, running custom scripts and updating software, etc. However, to perform these tasks manually, it takes more time and effort.

Here, our Support Engineers recommend CRON job to automate such daily tasks. Cron job executes a task at an interval or at a specific time.

Usually, cron works as a time-based scheduler for Linux-based operating systems. So, we can control the minute, hour, day, etc, that it will execute.

Also, each user has their own crontab. To list the cron job of a particular user,

crontab -u USER -l 

Moreover, in order to notify the user whether the command is running successfully, cronjob has a feature that mails the output to the owner of the crontab. It can even send details to the user named in the MAILTO environment variable in the crontab.

To add the MAILTO variable, we add ‘MAILTO=username@domain.com’ into the crontab file.

In cPanel, we can add the MAILTO variable as follows.

 

 

How we fix common Cron job errors

At Bobcares, where we have more than a decade of expertise in managing servers, we see many customers face problems while receiving cron job mails.

Now let’s see the major reasons for cron job-related errors and how our Support Engineers fix the top errors.

 

1. Incorrect settings in the crontab file

Usually, Cron will send the STDOUT and STDERR from the script to email.

Recently, one of our customers had a problem with crontab not sending emails. He had set up a backup script to back up data on his server using an hourly cron.

On checking, our Support Engineers found that he had set up script in a way to redirect output to a file. So there wasn’t any data to send. Therefore, we tweaked the settings in the crontab file as:

0 * * * * script.sh >> /var/log/script.log

This started logging the output to the log file and sending the error messages to the email.

 

2. Missing email address

Similarly, many customers experience the problem of crontab not sending email because of the wrong email address assigned to ‘’MAILTO=”. The crontab can’t send email to the mentioned email address without having a proper email server.

Also, if the email address provided is incorrect as it contains typo mistakes, the email sending fails.

In addition, the crontab allows you to set up MAILTO=root, then the mail will automatically be sent to the default email address of the root user.

In one of the servers, the customer has specified an invalid email address in the .forward file. As a result, all root mails failed including the mails from the crontab too.

So, our Support Engineers logged in to the server as a root user and added the correct email address to /root/.forward file there.

That solved the problem and mail started working fine.

 

[Need assistance to fix cron job errors? We’ll help you.]

 

Conclusion

In short, crontab not sending email error can happen due to missing root mail forwarder or due to the bad settings in the crontab file. Today, we saw the topmost reason for the cronjob errors and how our Support Engineers fixed it.



Source link

Author

Write A Comment