Page 1 of 1

How to echo errors to an out file using vsql?

Posted: Wed Sep 18, 2013 7:48 pm
by usli06
Hello,

I need to echo errors to an out file when running a vsql script. I cannot get errors to echo out.

Example:

Here is my script:

Code: Select all

[dbadmin@Vertica1 ~]$ cat test.sql
\o /home/dbadmin/test.log
create user test;
\o
The first time I run it, out out file contains the create user text as expected:

Code: Select all

[dbadmin@Vertica1 ~]$ vsql -f test.sql
[dbadmin@Vertica1 ~]$ cat /home/dbadmin/test.log
CREATE USER
But when I run it the second time, the error is not in the log file:

Code: Select all

[dbadmin@Vertica1 ~]$ vsql -f test.sql
vsql:test.sql:2: ROLLBACK 5403:  User/role "test" already exists
[dbadmin@Vertica1 ~]$ cat /home/dbadmin/test.log
I don't want the error to go to the screen, I want it in the log file.

Anyone know how to do this?

Re: How to echo errors to an out file using vsql?

Posted: Wed Sep 18, 2013 7:54 pm
by JimKnicely
Try:

nohup vsql -f test.sql > /home/dbadmin/test.log

Re: How to echo errors to an out file using vsql?

Posted: Wed Sep 18, 2013 8:21 pm
by id10t
Hi!

Try:

Code: Select all

vsql -f script.sql 2> errorl.logs
PS
IMO: nohup a wrong tool.
nohup - run a command immune to hangups, with output to a non-tty

Re: How to echo errors to an out file using vsql?

Posted: Thu Sep 19, 2013 6:41 am
by usli06
Thanks, guys!

But if I some additional items to the vsql script neither method gives the expected results.

Code: Select all

[dbadmin@Vertica1 ~]$ cat test.sql
\o /home/dbadmin/test.log
select current_database(), now();
\timing
create user test;
\o
knicely87's way:

Code: Select all

[dbadmin@Vertica1 ~]$ cat test.log
Timing is on.
vsql:test.sql:4: ROLLBACK 5403:  User/role "test" already exists
----------------------
 VMart            | 2013-09-18 22:38:27.911799-07
(1 row)
sKwa's way:

Code: Select all

[dbadmin@Vertica1 ~]$ vsql -f test.sql 2> test.log
Timing is on.
[dbadmin@Vertica1 ~]$ cat test.log
vsql:test.sql:4: ROLLBACK 5403:  User/role "test" already exists
----+-------------------------------
 VMart            | 2013-09-18 22:35:33.621687-07
(1 row)
Hmm. Shouldn't the result of the SELECT statement appear before the CREATE TABLE error?

Re: How to echo errors to an out file using vsql?

Posted: Thu Sep 19, 2013 7:09 am
by JimKnicely
Good point, sKwa! How about this one?

vsql -f test.sql &>> test.log

Re: How to echo errors to an out file using vsql?

Posted: Thu Sep 19, 2013 11:28 am
by id10t
Hi!

>> How about this one?
Also may be a solution, depends on needs.
1. Your variant isn't equal to my: your solution appends output while my always rewrite a file or creates a new one.
2. Also its a new syntax of Bash 4 for redirecting all streams (STDERR and STDOUT) to a single output, while usli asked for STDERR only.

NOTE: "&>outfile" is a Bash4 code and not portable. The way to go portable better is ">outfile 2>&1"

>> Shouldn't the result of the SELECT statement appear before the CREATE TABLE error?
No. STDERR and STDOUT are independent and parallel streams, while monitor/file/output device is a single device.
It's OS decides when to flush STDERR/STDOUT buffers and have similarity to locks in DB (because you have only single device for writing or only one table to write in it).

Re: How to echo errors to an out file using vsql?

Posted: Thu Sep 19, 2013 3:02 pm
by usli06
Thanks, guys. Great explanation sKwa. I guess I wasn't specific enough. I just wanted a log file that shows every command in the script as well as the output (including both errors and normal success out put from those commands). But you guys gave me what I needed. Thanks!!!