Cron and bash script with sendmail

Issues related to applications and software problems
Post Reply
Gutts
Posts: 5
Joined: 2015/02/27 15:02:03

Cron and bash script with sendmail

Post by Gutts » 2015/02/27 15:14:32

Hi all!
I wrote a little bash script to make some backups. It contains an email notification whether backup was done successfully or failed.
Here is a part of the script:

##Checks the logfile for errors and creates an email notification
err=`wc -l $DATE.error.log | awk {'print $1'}`
if [[ $err != 0 ]]; then
(echo "Subject: Jboss logrotate problem"; cat $DATE.error.log;) | /usr/sbin/sendmail admin@myserver.com
else
(echo "Subject: Successful Jboss logrotate on Jboss servers"; cat hosts.cfg;) | /usr/sbin/sendmail admin@myserver.com
fi

When I run it manually, everything is ok. I receive one email notification.
But when I add this script to crontab I see a lot of the same notification emails in my mailbox (screenshot attached).

cat /etc/cron.d/get_jboss_logs
MAILTO=admin@myserver.com
GETLOGS=/home/user/get_jboss_logs.sh
* */12 * * * user $GETLOGS

Why does it happen?
Attachments
noname.png
noname.png (86.51 KiB) Viewed 2787 times

gerald_clark
Posts: 10642
Joined: 2005/08/05 15:19:54
Location: Northern Illinois, USA

Re: Cron and bash script with sendmail

Post by gerald_clark » 2015/02/27 16:23:57

Because you told it to run every minute for 2 hours each day.

aks
Posts: 3073
Joined: 2014/09/20 11:22:14

Re: Cron and bash script with sendmail

Post by aks » 2015/02/27 20:31:01

Possibly (the 2 minute problem), but I'd suggest wrapping the parenthesis with a $, as:
err=$(wc -l $DATE.error.log | awk {'print $1'}) # backticks are so last decade!
if [[ $err != 0 ]]; then
$(echo "Subject: Jboss logrotate problem"; cat $DATE.error.log;) | /usr/sbin/sendmail admin@myserver.com
else
$(echo "Subject: Successful Jboss logrotate on Jboss servers"; cat hosts.cfg;) | /usr/sbin/sendmail admin@myserver.com
fi
then the stuff inside the () will execute in a subprocess, ready to be sent via sedmail (although I hope you're using postfix by now and not "real" sendmail daemons).

Whoever
Posts: 1361
Joined: 2013/09/06 03:12:10

Re: Cron and bash script with sendmail

Post by Whoever » 2015/02/28 04:49:58

aks wrote:Possibly (the 2 minute problem), but I'd suggest wrapping the parenthesis with a $, as:
err=$(wc -l $DATE.error.log | awk {'print $1'}) # backticks are so last decade!
if [[ $err != 0 ]]; then
$(echo "Subject: Jboss logrotate problem"; cat $DATE.error.log;) | /usr/sbin/sendmail admin@myserver.com
else
$(echo "Subject: Successful Jboss logrotate on Jboss servers"; cat hosts.cfg;) | /usr/sbin/sendmail admin@myserver.com
fi
then the stuff inside the () will execute in a subprocess, ready to be sent via sedmail (although I hope you're using postfix by now and not "real" sendmail daemons).
You don't want some of those "$"s:

Code: Select all

if [[ $err != 0 ]]; then
(echo "Subject: Jboss logrotate problem"; cat $DATE.error.log;) | /usr/sbin/sendmail admin@myserver.com
else
(echo "Subject: Successful Jboss logrotate on Jboss servers"; cat hosts.cfg;) | /usr/sbin/sendmail admin@myserver.com
fi
Also, as you want a numeric comparison, you should use "-ne" instead of "!=".

Code: Select all

if [[ $err -ne 0 ]]; then 

User avatar
TrevorH
Site Admin
Posts: 33216
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Cron and bash script with sendmail

Post by TrevorH » 2015/02/28 14:07:06

The other question is why are you doing it this way in the first place when CentOS ships (and installs by default I believe) the logrotate package. Just set up a config file in /etc/logrotate.d to do what you want and it will do it for you.
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke

Post Reply