LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-26-2018, 01:27 AM   #1
preet786
LQ Newbie
 
Registered: Sep 2018
Posts: 3

Rep: Reputation: Disabled
Cool Script to get fail when there is an error in log file


Hi

I am not very good at shell scripting.and need you help.
Requirement is to write a shell script which which should get fail if there is any type of error/exception found in the log and the job should get failed without proceeding further.
 
Old 09-26-2018, 02:12 AM   #2
Honest Abe
Member
 
Registered: May 2018
Distribution: CentOS 7, OpenSUSE 15
Posts: 420
Blog Entries: 1

Rep: Reputation: 202Reputation: 202Reputation: 202
1. We don't answer homework questions, but happy to assist if you show us your efforts.
2. Your requirement is pretty muddled. Please rephrase it better with examples.
3. Have a look here and here for questionnaire guidelines
 
Old 09-26-2018, 02:19 AM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
With this information, all I could do is writing pseudo code. I assume that the log file is a text file. My solution:

Code:
if grep -q ERROR-TEXT LOGFILE
then
    FAIL-JOB
fi
To write better code, I would need to know:

What is "any type of error/exception"? For example: Does the word "ERROR" occur in the log file? But "ERROR" might be part of a non-error message. Is there a certain code that identifies an error or exception? Do log messages have a precise structure with a specific error field? etc.

Which job should get failed?

How can this job be failed - sending it a signal? Writing something to the job's input? Executing a certain program?

Last edited by berndbausch; 09-26-2018 at 02:20 AM.
 
Old 09-26-2018, 02:47 AM   #4
preet786
LQ Newbie
 
Registered: Sep 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
Arrow

Quote:
Originally Posted by berndbausch View Post
With this information, all I could do is writing pseudo code. I assume that the log file is a text file. My solution:

Code:
if grep -q ERROR-TEXT LOGFILE
then
    FAIL-JOB
fi
To write better code, I would need to know:

What is "any type of error/exception"? For example: Does the word "ERROR" occur in the log file? But "ERROR" might be part of a non-error message. Is there a certain code that identifies an error or exception? Do log messages have a precise structure with a specific error field? etc.

Which job should get failed?

How can this job be failed - sending it a signal? Writing something to the job's input? Executing a certain program?

===

The error can be any oracle error/java exception error.
So,Ii want to grep error, ora,fatal,exception etc in the log file.
there is a script runs via a job scheduler and I want whenever there is any such error the job should get fail.
The job is running in the server with respective process id.
So, in case of any error identified in the log file , job should get fail immediately.
 
Old 09-26-2018, 03:05 AM   #5
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
If I've quite understood you, you would like to stop the job according to what the log is displaying?
Isn't the job aware of what it asks the log to store? Because if it is, maybe the job could stop itself while generating the corresponding log?
Anyway, as stated by Honest Abe in post#2, you should show us some efforts...but so far you have just provided us with requirements.
 
Old 09-26-2018, 05:57 AM   #6
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Amended script, still lots of gaps for you to fill:
Code:
if grep -q -e error -e ora -e fatal -e exception $LOGFILE
then
    kill $JOB_PID
fi
You will have to correct the grep search. The way I wrote it, you will have plenty of false positives. You need to analyze the error messages and craft water-tight regular expressions that find those errors, and only those errors.

Another gap is the job PID. That should be easy, but you need to find out how to stop it gracefully. I just assume that a termination signal does the job.

Write your script, test it, and if you get stuck, ask again.

Last edited by berndbausch; 09-26-2018 at 05:58 AM.
 
Old 09-26-2018, 05:58 AM   #7
preet786
LQ Newbie
 
Registered: Sep 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
$ cat xyz.ksh
#!/bin/ksh

V_LOG = /cs/mantas/fccm801/database/db_tools/logs/äbc.log
V_ERR = egrep -wi 'Exception|Error|ORA-*|FATAL|FAIL' $V_LOG

STATUS=$?

case $STATUS in
1)
echo "Exception raised: of type Exception"
;;
2)
echo "Exception raised: of type SQLException"
;;
esac

return $STATUS

if ($V_ERR -gt 0)
then
echo "Check detailed error in the log file"
exit -1;
else
echo "Job is successfully completed"
 
Old 09-26-2018, 08:50 AM   #8
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
It appears as though you're guessing. How does any value get assigned to $STATUS? I don't think we understand what you mean by 'fail(ed)'. Your script doesn't show anything related to killing a process. It appears it only gives a message. I'm not sure if the -gt 0 will get you anywhere, because the variable is nonnumeric.

This might get you going a bit more: http://www.well.ox.ac.uk/~johnb/comp/unix/ksh.html
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
NFS Large File Copies Fail - Error writing to file: Input/output error deck- Linux - Server 10 08-01-2022 02:30 AM
[SOLVED] script used force-all on a i386 deb file for a armhf system, now installation activities fail. BryanFRitt Linux - Software 1 12-23-2017 10:06 PM
[SOLVED] libperl.so.5.18.2 Error log in kern.log file mahi_nix Ubuntu 2 06-18-2014 07:12 AM
Shell script to monitor the log file & kill the process if log is not updating. milu_k Programming 5 07-19-2012 08:23 AM
Root crontab running script, log re-directs fail. Explanation needed. mattst Linux - General 5 10-27-2011 09:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration