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?
Cron and bash script with sendmail
Cron and bash script with sendmail
- Attachments
-
- noname.png (86.51 KiB) Viewed 2787 times
-
- Posts: 10642
- Joined: 2005/08/05 15:19:54
- Location: Northern Illinois, USA
Re: Cron and bash script with sendmail
Because you told it to run every minute for 2 hours each day.
Re: Cron and bash script with sendmail
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).
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).
Re: Cron and bash script with sendmail
You don't want some of those "$"s: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).
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
Code: Select all
if [[ $err -ne 0 ]]; then
Re: Cron and bash script with sendmail
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
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