Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sendmail is not working with crontab (bash)

I have created one disk cleanup script which after cleanup sends a status email. now when I run this through command line, it executes perfectly but through cronjob its not able to send staus mail rest the script is working fine though. I have read many solutions in google but nothing is working for me. I am using Bash on my Ubuntu machine. here is sendmail part of my script.

export CONTENT="/root/cleanup/cleanup.htm"                                           
export SUBJECT="Disk Space Clean Up Process : Completed @ $date_time"

(echo "Subject: $SUBJECT"
echo "`cat sendmail_list.txt`"
echo "MIME-Version: 1.0"
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat $CONTENT
)|/usr/sbin/sendmail -t 

please help me know the solution...thanks

like image 708
KnowledgeSeeeker Avatar asked May 19 '14 11:05

KnowledgeSeeeker


People also ask

Why crontab can’t send email?

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.

How do I stop a cron script from sending to email?

By default, any output produced by things run by cron is sent by e-mail to the owner of the crontab. To disable this, either make sure your scripts do not produce any output, or redirect it to /dev/null in your crontab, or add MAILTO="" at the top of your crontab.

How to add mailto environment variable in 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 ‘[email protected]’ into the crontab file. In cPanel, we can add the MAILTO variable as follows.

Why is my crontab not working?

Why is my crontab not working, and how can I troubleshoot it? Your code has multiple problems: 1. the "echo TESST" is effectively ignored because you also have a file redirection for stdin; 2. the MAILTO already takes care of mailing the output, you don't need to call sendmail from the script anymore (just send the contents of the mail to stdout).


2 Answers

Please add /usr/sbin before sendmail in sh file:

/usr/sbin/sendmail "[email protected]" < file.txt 

Hope it helps https://stackoverflow.com/editing-help

like image 188
Lenix Avatar answered Oct 21 '22 10:10

Lenix


You need a blank line between the message header and the body.

{
    echo "Subject: $SUBJECT"
    echo "$(< sendmail_list.txt)"
    echo "MIME-Version: 1.0"
    echo "Content-Type: text/html"
    echo "Content-Disposition: inline"
    echo ""
    cat $CONTENT
} | /usr/sbin/sendmail -t 

A couple of other things:

  • you don't need a subshell here, so I changed the surrounding parentheses to braces
  • since this is bash, there's a shorthand for $(cat file) -- $(< file)
like image 3
glenn jackman Avatar answered Oct 21 '22 10:10

glenn jackman