How to Create LOG and Validate for this Script

Hi all,

This is my Script

#!/bin/bash
current_date=`date +%d-%m-%Y_%H.%M.%S`
Date=`date +%d%m%Y`
Lfriday=`date +%d%m%Y -d last-friday`
RootPath=/var/domaincount/com

mysql zonefile<<EOFMYSQL
CREATE TABLE com$Date(domain VARCHAR(255),col2 VARCHAR(255),col3 VARCHAR(255),col4 VARCHAR(255),id INT(25) NOT NULL AUTO_INCREMENT, KEY (id));
LOAD DATA INFILE '$RootPath/$today.com' INTO TABLE com$Date FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\n';
delete from com$Date limit 35;
delete from com$Date order by id desc limit 2;
select count(distinct(SUBSTRING_INDEX(domain, '.',-1))) from com$Date INTO OUTFILE '$RootPath/$today.count';
select count(distinct(SUBSTRING_INDEX(a.domain, '.',-1))) from com$Date a, com$Lfriday b where a.domain=b.domain INTO OUTFILE '$RootPath/$today.overall';

for normal script i can create log, but in that i used mysql queries, suppose for example one sql query not executed properly means how to intimate this issue to administrator..like that i need to log for each query.

how to create, is it possible??

You mean, you want it to send a mail? or what?

yes mail alert

Something like:

#!/bin/bash
current_date=`date +%d-%m-%Y_%H.%M.%S`
Date=`date +%d%m%Y`
Lfriday=`date +%d%m%Y -d last-friday`
RootPath=/var/domaincount/com

mysql zonefile<<EOFMYSQL || echo "query failed" | sendmail 'username@where'
CREATE TABLE com$Date(domain VARCHAR(255),col2 VARCHAR(255),col3 VARCHAR(255),col4 VARCHAR(255),id INT(25) NOT NULL AUTO_INCREMENT, KEY (id));
LOAD DATA INFILE '$RootPath/$today.com' INTO TABLE com$Date FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\n';
delete from com$Date limit 35;
delete from com$Date order by id desc limit 2;
select count(distinct(SUBSTRING_INDEX(domain, '.',-1))) from com$Date INTO OUTFILE '$RootPath/$today.count';
select count(distinct(SUBSTRING_INDEX(a.domain, '.',-1))) from com$Date a, com$Lfriday b where a.domain=b.domain INTO OUTFILE '$RootPath/$today.overall';
EOFMYSQL

To have sendmail, you have to have an MTA of some sort installed. If you have none and don't want to bother setting up a mail server, ssmtp is a simple stub of a mailserver that only sends, and sends by logging in as a mail client to something else.

LOG=/var/tmp/log/com/comcount$current_date.log

mysql zonefile<<EOFMYSQL || echo "query failed" >> $LOG
CREATE TABLE com$Today(domain VARCHA(255),col2 VARCHAR(255),col3 VARCHAR(255),col4 VARCHAR(255),id INT(25) NOT NULL AUTO_INCREMENT, KEY (id));
LOAD DATA INFILE '$RootPath/$Today.com' INTO TABLE com$Today FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\n';
delete from com$Today limit 35;
delete from com$Today order by id desc limit 2;
select count(distinct(SUBSTRING_INDEX(domain, '.',-1))) from com$Today INTO OUTFILE '$RootPath/$Today.count';
select count(distinct(SUBSTRING_INDEX(a.domain, '.',-1))) from com$Today a, com$Lfriday b where a.domain=b.domain INTO OUTFILE '$RootPath/$Today.overall';

cat $LOG | mail -s " com Tld Count log" 07anis@gmail.com

i give like this but its not writting into the log file..any idea what mistake iam doing??

You forgot the EOFMYSQL I labelled in red, which is causing lines below it to be interpreted as part of the mysql script and not the shell one.

You do have sendmail actually working, right?

Sorry Man..I missed to post here..but using EOFMYSQL statement only i run myscript -- i got error only

ya 100% working

Could you show what your script actually is, then?

#!/bin/bash
current_date=`date +%d-%m-%Y_%H.%M.%S`
Today=`date +%d%m%Y`
Lfriday=`date +%d%m%Y -d last-friday`
RootPath=/var/domaincount/com
ch=domaincount
LOG=/var/tmp/Intelliscanlog/com/comcount$current_date.log

echo Intelliscan Process started for .COM TLD $current_date >> $LOG

#################################################################################################
 Using Wget Downloading the Zone files it will try only one time
if ! wget --tries=1 --ftp-user=USERNAME --ftp-password=PASSWORD ftp://ftp.URL/zone.gz

#then
 #  echo Download Not Success .COM Domain count Failed With Error >> $LOG
  # exit 1
#fi
###The downloaded file in Gunzip format from that we need to unzip and start the domain count process####

gunzip com.zone.gz

mv com.zone $RootPath/$Today.com

cd /var

chown -R mysql:mysql $ch

cd $RootPath

###### It will start the Count ####ootPatH
mysql zonefile<<EOFMYSQL || echo "query failed" >> $LOG
CREATE TABLE com$Today(domain VARCHA(255),col2 VARCHAR(255),col3 VARCHAR(255),col4 VARCHAR(255),id INT(25) NOT NULL AUTO_INCREMENT, KEY (id));
LOAD DATA INFILE '$RootPath/$Today.com' INTO TABLE com$Today FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\n';
delete from com$Today limit 35;
delete from com$Today order by id desc limit 2;
select count(distinct(SUBSTRING_INDEX(domain, '.',-1))) from com$Today INTO OUTFILE '$RootPath/$Today.count';
select count(distinct(SUBSTRING_INDEX(a.domain, '.',-1))) from com$Today a, com$Lfriday b where a.domain=b.domain INTO OUTFILE '$RootPath/$Today.overall';
EOFMYSQL

### Calculation Part
a=$(< $RootPath/$today.count)
b=$(< $RootPath/$Lfriday.count)
c=$(< $RootPath/$Today.overall)


echo "$current_date Today Count For COM TlD $a" >> $LOG
echo "$current_date New Registration Domain Counts $((c - a))" >> $LOG
gdk-pixbuecho "$current_date Deleted Domain Counts $((c - b))" >> $LOG cat $LOG | mail -s "BIZ Tld Count log" 07anis@gmail.comf >= 0.8.0

This is the exact code... This code is not new for you..in my previous post i post the same code, but in that slight changes instead of AWK i am trying MYSQL it works well and reduced my problem,

but my curious is if any error happened in mysql queries how to make that error as log, need your guidance to solve this thread

Thanks alot Guys
Anish kumar.V

Hmmm. I suspect if errors happen, it prints an error message to stderr.

Above all the queries, you could do

exec 2>errlog

and all errors will be logged to that file. Unfortunately that doesn't tell you exactly which one had the error, or even if.

I don't understand why the || echo bits aren't being run. For some reason mysql must not be returning error when it fails to execute a statement. Maybe it's because of the here-document, try || ( echo "query failed" >> $LOG )

exec 2>errlog

How to include in this script,

because this also not working

|| ( echo "query failed" >> $LOG ) 

and i have another i doubt ,

suppose if one query not execute properly or throw error means how to stop the further process.

CREATE TABLE com$Today(domain VARCHA(255),col2 VARCHAR(255),col3 VARCHAR(255),col4 VARCHAR(255),id INT(25) NOT NULL AUTO_INCREMENT, KEY (id));

in this query i made a mistake instead of VARCHAR i used VARCHA

it show error but after this error its continously moving to next query.

i dont want like this once the error happened in any of the query then the next process is quit the script and triggered mail to admin the process stopped like that.

Is it possible ??

Thanks Corona688 as a senior your doing such a helpful to me and most of the beginners , and you are very down to earth character thanks a lot for your patience to read my bad english and understand my queries. :slight_smile: