utkarsh tyagi utkarsh tyagi - 3 months ago 15
Bash Question

How to capture the logs from dbaccess

I am trying to create a script to check the connectivity between server databases!

For that I need to capture the logs, but the logs are showing on the screen and not in the file.

Files:

main.scr



#!/bin/ksh
server[0]="database1_tcp"
server[1]="database2_tcp"
server[2]="database3_tcp"
for i in "${server[@]}";
do
query.scr $i>>log_test.dat
done


query.scr



#!/bin/ksh
DBACCESS testdb << EOSQLF
SELECT *
FROM testdb@$1:some_table
EOSQLF


log_test.dat:



checking the connectivity for server: database1_tcp

field1 field2

A A

***********************************************
checking the connectivity for server: database2_tcp

***********************************************
checking the connectivity for server: database3_tcp


field1 field2

A A

***********************************************


Terminal output



What comes on screen when I run main.scr:

Database selected.


1 row(s) retrieved.


Database closed.


Database selected.


908: Attempt to connect to database server (database2_tcp) failed.
Error in line 3
Near character position 1

Database closed.


Database selected.


1 row(s) retrieved.


Database closed.


Is there a way to direct the messages coming on screen to some log file, or can you suggest a better way to do it?

Answer

As shellter suggested, DB-Access (dbaccess) writes some of its output to standard error and some to standard output. (Note that although dbaccess writes error messages to standard error, it also writes other status information there as well.) You need to capture both, so you need to revise your script using 2>&1 to send file descriptor 2 (aka standard error) to the same place that file descriptor 1 (aka standard output) is going:

#!/bin/ksh
server[0]="database1_tcp"
server[1]="database2_tcp"
server[2]="database3_tcp"
for i in "${server[@]}"
do
    query.scr $i >>log_test.dat 2>&1
done

(The same redirection works in 7th Edition Unix Bourne shell from circa 1979, and its inheritors such as Bash and Korn shell. The URL is formally to the Bash docs; this part works the same in Korn shell too.)

You might find that my SQLCMD program is better in this respect. It is designed for use in scripts, and writes normal output to standard output and errors to standard error, and is less verbose in the first place (unless you tweak it to be verbose). It bears no relation (other than name and general purpose) to Microsoft's johnny-come-lately program of the same name — but MS manages to dominate the searches if you're not careful.

Comments