I am trying to check a table and send email based on the number of records. If the records are zero for the particular date then success email shall go out. If there is any record for the particular day then error messages should go out. My problem is - the opposite is happening. even though there are no records, error mail is going out.
Below is the script
#!/bin/bash
export PGPASSWORD=xyz
TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
mailid='xyz#gmail.com'
cd /opt/postgres/pgsql/bin
vartest=`./psql -U user -h host -p port umrm -t -c "select count(*) from dataprocess_errors where error_date = current_date"` >> /opt/rmapp/test_error.log
if [$vartest -eq 0]
then
echo 'Files processed without errors' | mutt -s "Processing Success `date`" $mailid $attached >> /opt/rmapp/test_error.log
else
error=`./psql -U user -h host -p port umrm-t -c "select count (*) from dataprocess_errors where error_date = current_date and error_message like '%More than 25 employees%';"` >> /opt/rmapp/test_error.log
if [$error -eq 0]
then
echo 'Please check the table and fix the errorneous records' | mutt -s "Alert!! $vartest records failed in processing `date`" $mailid $attached >> /opt/rmapp/test_error.log
else
echo 'Please check the table' | mutt -s "Alert!!! processing stopped `date`" $mailid $attached >> /opt/rmapp/test_error.log
fi
fi
echo "Error Check completed at : $TIMESTAMP" >> /opt/rmapp/test_error.log
the second if condition in the else section is to identify the different kind of errors based on the output of variable "error".
I am a noob in shell scripting so kindly help.
Thanks in advance for all the help.
Considering your psql command is working fine, you have only one issue in above script. In both if conditions space after '[' and before ']' is missing.
So your if conditions should be like :
if [ $vartest -eq 0 ]
and
if [ $error -eq 0 ]
In your lines
vartest=`./psql -U rmdbprd -h atla-db-rmgr-prd-priv.com -p 5432 umrm -t -c "select count(*) from dataprocess_errors where error_date = current_date"`
and
error=`./psql -U rmdbprd -h atla-db-rmgr-prd-priv.com -p 5432 umrm-t -c "sele ........
you are getting the standard out from the command - not the error code
to get the error code you can (as an example):
./psql -U rmdbprd -h atla-db-rmgr-prd-priv.com -p 5432 umrm -t -c "select count(*) from dataprocess_errors where error_date = current_date"
if [ $? -ne 0 ];
then
echo "there was an error on above command"
fi
note: the if must follow directly after the command you are testing the error code on
REF: https://en.wikipedia.org/wiki/Bash_(Unix_shell)
Related
When I try to run this .sh script
#!/bin/sh
expect -c "
spawn ssh -o StrictHostKeyChecking=accept-new postgres#postgres -p 23
expect \"password: \"
send \"postgres\r\"
expect \":~$\r\"
send \"psql -U postgres -c "select * from pg_switch_wal();" -Fp\r\"
#send \"pg_basebackup -h postgres -p 5432 -U postgres -D /backup/$1 -Fp\r\"
#send \"psql -U postgres -c "select * from pg_switch_wal();" -Fp\r\"
expect \"postgres: \"
send \"postgres\r\"
expect \":~$\r\"
send \"tar -cvf /backup/$1.tar /backup/$1/ \r\"
expect \":~$\r\"
send \"rm -rf /backup/$1/ \r\"
expect \":~$\r\"
send \"exit 0\r\"
interact "
It returns the below error.
bash-5.1# ./db_incrementalbackup.sh ./db_incrementalbackup.sh: line 8: syntax error: unexpected "("
Could you please advise where I'm wrong?
I have a function block as below which is getting called from somewhere. The call is happening fine but when it's executing any of the below psql check commands eg:
local db_availability_check_88=`psql -h 10.95.187.88 -p 5444 -c "\pset tuples_only" -c "select pg_is_in_recovery;"|grep -v "Tuples"`
The script terminates immediately if the script is unable to communicate to the database via the above psql command. I want thee script to continue its execution. If the connectivity fails it should continue with the next statements.
check_db_status_function () {
echo "entered function block db_status"
local db_availability_check_67=`psql -h 10.95.167.87 -p 5444 -c "\pset tuples_only" -c "select pg_is_in_recovery;"|grep -v "Tuples"`
local db_availability_check_68=`psql -h 10.95.167.88 -p 5444 -c "\pset tuples_only" -c "select pg_is_in_recovery;"|grep -v "Tuples"`
local db_availability_check_69=`psql -h 10.95.167.89 -p 5444 -c "\pset tuples_only" -c "select pg_is_in_recovery;"|grep -v "Tuples"`
if [[ ( $a1 = "down" ) && ( $a3 != "primary" ) && ( $db_availability_check_67 = 't' ) ]];
then
echo "pgPool services are down on the node:$a2"
echo "Promoting..."
/u01/edb/pgpool3.6/bin/pcp_attach_node -w -U pcpuser -h localhost -p 9898 $a0
fi
The script o/p is as :
Nested block 1
check_db_stat
entered function block db_status
psql.bin: could not connect to server: Connection refused
Is the server running on host "10.95.167.88" and accepting
TCP/IP connections on port 5444?
Thanks,
Sandeep
I am trying to store a Greenplum query result into a csv but it is storing only one row. I want to store multiple rows. Can anyone help with this.
I tried the below already but it is storing only one row.
r= `psql -A -t -q -h $hostname -U $username -d $dbname <<-THE_END
COPY(select * From ${gschema}.${gtable} order by ${key} limit 3) TO STDOUT with NULL as ' '
THE_END` > /home/gp.csv
I also tried below which is storing the result but I am unable to pass table name as parameter in the below query. For standard table names the output is as desired.
psql -h $hostname -U $username -d $dbname -c "COPY (select * from table_name order by key limit 3) TO STDOUT "> /home/gp.csv
Can anyone help me with this please.
please note that I am trying to embed the above in a shell script
Try this:
psql -A -t -q -h $hostname -U $username -d $dbname -c "select * from $gschema.$gtable order by $key limit 3" > /home/gp.csv
You could also put the SQL in a file:
example.sql
select * from :gschema.:gtable order by :key limit 3
And then to use the sql file:
psql -A -t -q -h $hostname -U $username -d $dbname -f example.sql -v gschema=public -v gtable=customer -v key=id > /home/gp.csv
I am trying to achieve this kind of formatted data from my select statement and have it send via database mail in SQL SERVER 2000. I know how to do this in SQL 2008.
Column1 | Column 2
------------------
Value 1 | Value 2
Value 1 | Value 2
Value 1 | Value 2
I have realized that this can not be done. To achieve this I created .bat file and stored procedures and sendmail utility.
First call this in your .bat file
osql -S %ServerName% -d %dbname% -U %username% -P %password% -Q "EXEC
" -o %TempFile1% -s "|" -w 5000 >>%logfile%
and then call
C:\Tools\SendEmail\sendEmail.exe -f -t -u
"[%ServerName%] Number of Records Processed: %lineCount%" -m "Data
File Generated: %DataFile%" -a %DataFile% -s
I have a script that I run nightly in cron to backup some postgres databases for several hosts on my network. I have a way of getting alerted that the script fails by leveraging the exit status, but it doesn't tell me WHY it failed.
Based off the following code, how can I capture any errors that occur when the script is run, and email them to me so I can have a better understanding of what happened.
FILEDATE=`date '+%Y-%m-%d'`
BASEDIR=/u1/$1/db_dumps
PGDUMP=/path/to/pg_dump
HOST=$1
DB=$2
if [ $DB == all ]
then
for ALLDUMPS in db1 db2 db3
do
ssh root#$HOST "env PGUSER=pguser PGPASSWORD=pgpassword $PGDUMP -Fc $ALLDUMPS" | pbzip2 > $BASEDIR/$FILEDATE-$HOST-$ALLDUMPS.dump.bz2
if [ $? -ne 0 ]
then mutt -s "dbdumper could not create a backup of $ALLDUMP from $HOST" me#myemail.com < /dev/null
fi
done
else
ssh root#$HOST "env PGUSER=pguser PGPASSWORD=pgpassword $PGDUMP -Fc $DB" | pbzip2 > $BASEDIR/$FILEDATE-$HOST-$DB.dump.bz2
if [ $? -ne 0 ]
then mutt -s "dbdumper failed to create a backup of $DB from $HOST" me#myemail.com < /dev/null
fi
fi
Capture stderr from the ssh command, and email that to yourself.
stderr=$( ssh root#$HOST "env PGUSER=user PGPASSWORD=pw $PGDUMP -Fc $ALLDUMPS" |
pbzip2 2>&1 > "$BASEDIR/$FILEDATE-$HOST-$ALLDUMPS.dump.bz2" )
if [ $? -ne 0 ]; then
subj="dbdumper could not create a backup of $ALLDUMP from $HOST"
mutt -s "$subj" me#myemail.com <<< "$stderr"
fi