Capture crash out error report...


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Capture crash out error report...
# 1  
Old 12-29-2013
Capture crash out error report...

Google is not helpful here.
It is probably trivial but I can't get my head around it.
How do I capture an error which crashes out due to say a syntax error to a file?
(Note that I manually reset error.log to zero length.)

Consider this junk code:-
Code:
#!/bin/sh
# Launch as ./trap.sh 2>/tmp/error.log

# Capture this on program crash.
junk()
{
	echo "Some test text."
}

# DONE! This line works as required.
trap 'junk >> /tmp/error.log' EXIT

# Do nothing just added to test with.
for n in {0..20}
do
	: # Do nothing just a useless routine.
done

# Just added for the test, does nothing.
x="0"
y=0

# Also added for the test and does nothing.
for n in {0..4}
do
	printf "I am here...\n"
done

# THIS IS THE ACTIVE PART.
# Crash out here.
eval [ ( $x -= 0 ]
# ./trap.sh: line 22: syntax error near unexpected token `('
# ./trap.sh: line 22: `eval [ ( $x -= 0 ]'
# How do I capture this crashout error to a file INSIDE this code?

# The line below will never be reached.
printf "G0LCU is never seen."

If I launch the script, "./trap.sh<CR>", as stand alone then the results are wrong.
But if I launch as "./trap.sh 2>/tmp/error.log<CR>" then all is good...
Code:
AMIGA:barrywalker~> ./trap.sh
I am here...
I am here...
I am here...
I am here...
I am here...
./trap.sh: line 31: syntax error near unexpected token `('
./trap.sh: line 31: `eval [ ( $x -= 0 ]'
AMIGA:barrywalker~> cat < /tmp/error.log
Some test text.
AMIGA:barrywalker~> # This is NOT what I want.
AMIGA:barrywalker~> # But if I launch ./trap.sh 2>/tmp/error.log<CR>
AMIGA:barrywalker~> ./trap.sh 2>/tmp/error.log
I am here...
I am here...
I am here...
I am here...
I am here...
AMIGA:barrywalker~> cat < /tmp/error.log
./trap.sh: line 31: syntax error near unexpected token `('
./trap.sh: line 31: `eval [ ( $x -= 0 ]'
Some test text.
AMIGA:barrywalker~> # This IS what I want.
AMIGA:barrywalker~> _

How do I re-route the crash out error AND the junk information to a common file on program crash out?

TIA

(Happy New Year to all.)
# 2  
Old 12-30-2013
You can try something like...
Code:
cat err.sh
#!/bin/bash -x # -x can be omitted 
VAR=test
exec 2>> $HOME/output.log
if [ $VAR' == "test" ]; then
echo "nono"
fi
$ ./err.sh
+ VAR=test
+ exec
$ cat output.log
./err.sh: line 4: unexpected EOF while looking for matching `''
./err.sh: line 7: syntax error: unexpected end of file

This User Gave Thanks to Peasant For This Post:
# 3  
Old 12-30-2013
Exactly what I want, many thanks...

"exec" is the line...

Note that there is a _BUG_ just so that everyone is aware; consider this code again:-
NOTE the read line...
Code:
#!/bin/bash
# Launch as ./trap.sh 2>/tmp/error.log
exec 2>> /tmp/error.log
# Capture this on program crash.
junk()
{
	date
	echo "Some test text."
}

# DONE! This line works as required.
trap 'junk >> /tmp/error.log' EXIT

# Do nothing just added to test with.
echo "$0"
echo "$1"

# Just added for the test, does nothing.
x="0"
y=0

# Also added for the test and does nothing.
for n in {0..4}
do
	printf "I am here...\n"
done
# NOTE that the line below works but does NOT print the prompt nor what is typed in.
printf "Where is the prompt and KB input?\n"
read -p "Enter your text:- " -e kbinput
# We do have a workaround... ;o)
printf "However we do have a workaround...\n"
printf "Type in some text:- "
read kbinput
echo "$kbinput"
# THIS IS THE ACTIVE PART.
# Crash out here.
eval [ ( $x -= 0 ]
# ./trap.sh: line 22: syntax error near unexpected token `('
# ./trap.sh: line 22: `eval [ ( $x -= 0 ]'
# How do I capture this crashout error to a file INSIDE this code?

# The line below will never be reached.
printf "G0LCU is never seen."

AHA! So the prompt and typing is inside STDERR.
However the workaround is NOT a problem so I will go with your reply.
Results:-
Code:
Last login: Mon Dec 30 16:52:03 on ttys000
AMIGA:barrywalker~> > /tmp/error.log
AMIGA:barrywalker~> ./trap.sh
./trap.sh

I am here...
I am here...
I am here...
I am here...
I am here...
Where is the prompt and KB input?
However we do have a workaround...
Type in some text:- I can see the typing now...
I can see the typing now...
AMIGA:barrywalker~> cat < /tmp/error.log
Enter your text:- 1234567890qwertyuiop
./trap.sh: line 37: syntax error near unexpected token `('
./trap.sh: line 37: `eval [ ( $x -= 0 ]'
Mon 30 Dec 2013 16:56:22 GMT
Some test text.
AMIGA:barrywalker~> _

Again many thanks...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Capture error before pipe

Hi, I have a script that runs a tar command to standard out then pipes to a gzip: tar cfE - * | gzip -c > OUT.gz At the moment, even if the tar fails (e.g. because of lack of disk space), the gzip still runs successfully. Is there a way to make the whole line exit with a non-zero error... (6 Replies)
Discussion started by: Catullus
6 Replies

2. Shell Programming and Scripting

How to capture the error code a use it in error table?

Hello Everyone, I have written a file validation script in unix to compare the data and trigger file.My requirement is if the file validation fails,I need to upate the error details in a table ex:Below is the one of many validation checks i am doing if then echo "Actual count between... (3 Replies)
Discussion started by: karthik adiga
3 Replies

3. UNIX for Dummies Questions & Answers

How to Capture Informatica Error logs?

HI Team , Is there a way to capture information logs ( session or workflow ) and grep only ERROR or FAIL messages and send to email distro using mailx or sendmail option. I have around 200 sessions running on daily basis and this script has to run every day , and capture only error... (0 Replies)
Discussion started by: Perlbaby
0 Replies

4. Shell Programming and Scripting

Not able to capture error while using dialog

I am not able to capture errors while I am using dialog. For example: dialog --gauge "Verifying file..." 10 75 < <( while read LINE do hash=$(echo $LINE | cut -f1 -d' ') directory=$(echo $LINE | cut -c 34-) PCT=$(( 100*(++i)/n )) echo $PCT md5deep -a $hash $directory >... (3 Replies)
Discussion started by: yamanoorsai
3 Replies

5. UNIX for Dummies Questions & Answers

Capture Error In SQL Plus

Hi I am trying to fetch the data from Oracle Table More my_query.sql | sqlplus -s 'scott/tiger@OrcaleSID ' | sed 's///g;s///g' > sample_file Now if the table passed in the my_query.sql is wrong or there is some other error insied SQL how i can caputure that i tried More... (1 Reply)
Discussion started by: max_hammer
1 Replies

6. SCO

Crash error on my unix server

Hi there. Well i have a really bad problem with my server: UnixWare Version 5 Release 7 The system crash :wall: and show the error: Panic: Kernel-mode address fault on user address 0x00000004 :eek: If anyone knows about the reason of this error please give me a help Sorry by my english.... (3 Replies)
Discussion started by: danilosevilla
3 Replies

7. Shell Programming and Scripting

database instance Error capture

I wrote a script to capture some rows from the DB. However I want to capture the errors if the DB instance is down which usually produces errors like below. What should be my approach to kill script if the DB instance is down: DATABASE ERRORS: Msg 937, Level 14, State 1: Server... (2 Replies)
Discussion started by: moe458
2 Replies

8. Shell Programming and Scripting

Capture Schell script error

I work on AIX 5.x. I have a script which does lot of processing & calls multiple child scripts. How do I capture the error of the parent script if it fails? Thanks Sumeet (3 Replies)
Discussion started by: sumeet
3 Replies

9. Shell Programming and Scripting

Need to capture ERROR msg and stack trace

Hi all, I was hoping someone can point me in the right direction. I'm trying to filter out errors from a web log- any lines with ERROR in it. I know I could simply use the grep command to do this. However, there are times when a stack trace follows the error line. I would like to capture these... (2 Replies)
Discussion started by: gswhoops
2 Replies

10. Shell Programming and Scripting

sqlplus -s error capture

Hi folks a wee problem that requires some help. I am writing a script that allows me to automate the changing of a password in sqlplus the problem i encounter is that the code contains a case statement that allows the helpdesk to select whether to allow or deny access by switching the... (1 Reply)
Discussion started by: w33man
1 Replies
Login or Register to Ask a Question