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
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.
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.
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, 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).
Please add /usr/sbin
before sendmail in sh file:
/usr/sbin/sendmail "[email protected]" < file.txt
Hope it helps https://stackoverflow.com/editing-help
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:
$(cat file)
-- $(< file)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With