Script to get fail when there is an error in log file
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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.
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.
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.
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524
Rep:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.