-2

I have problem with sending mail in Crontab

#!/bin/sh
log_direc="/var/log/snort/alert"
email="vodeni1953@heroulo.com"
echo "TESST" | sendmail $email < output.txt`

It can run normal on command and possible to send mail. screenshot of e-mail

But when i put this script to Cron like this

MAILTO=vodeni1953@heroulo.com
* * * * * /bin/sh /home/weed/Desktop/test.sh

It looks like this second e-mail screenshot

How can I fix it? Thanks

Andrew Schulman
  • 8,561
  • 21
  • 31
  • 47
  • 4
    Does this answer your question? [Why is my crontab not working, and how can I troubleshoot it?](https://serverfault.com/questions/449651/why-is-my-crontab-not-working-and-how-can-i-troubleshoot-it) – Bob Mar 21 '21 at 17:08
  • 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). – Simon Richter Mar 22 '21 at 13:46

1 Answers1

0

Did you remove the PATH variable at the top of your crontab? If that's missing, it would explain why your script cannot find sendmail. A typical crontab has the following variables defined. I've added the line for your script as well.

# Global variables
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

* * * * * /bin/sh /home/weed/Desktop/test.sh

The MAILTO at the top of your crontab is typically used for cron to know where to mail things to. Keep individual configuration in your script.

Also, you're going to need the absolute path for the file you wish to send when running the script via cron. It seems like when you run it manually, you're in the directory you want it to execute in.

You're also using both a pipe to send the output of echo into sendmail as well as directing information into standard-in. Remove the standard-in direction and just put all the information you want sent on the left side of the pipe:

log="/var/log/snort/alert/output.txt"
email="vodeni1953@heroulo.com"
cat $log | sendmail $email

If you want to add something to the file you're sending, use the following:

echo -e "Some Header\n\n$(cat $log)" | sendmail $email
djsumdog
  • 1,060
  • 2
  • 16
  • 29
  • Thanks for advise but when i try tro delete MAILTO and fix the script to echo "TEST" | /usr/sbin/sendmail $email so it can send mail. I think the problem is it does not recognize sendmail method so it can't send .. – Lê Hyung Mar 21 '21 at 15:12
  • 1
    The problem is that the PATH variable is not set properly, as @djsumdog wrote. Your experiment confirms that. So follow the advice and put the PATH line back into your crontab file. Also put the MAILTO line back in so that you get errors from your cronjobs mailed to the root account. In addition, it would be a good idea to set PATH explicitly at the top of your script so it won't break that easily just because it is called with PATH set differently to what it expects. – Tilman Schmidt Mar 21 '21 at 18:29
  • @LêHyung PATH needs to be set in your crontab itself (or do what @Tilman-schmidt suggests and explicitly use `/usr/bin/sendmail` instead). I adjusted my answer so you can clearly see those variables are actually in the crontab with the script you have scheduled below it. Does it make sense now? – djsumdog Mar 21 '21 at 20:18