0

A process in a bash script I'm developing often fails quietly (it keeps running with no error code, but it writes the error in its log file).

As soon as the error appears in the log file I want the process restarted (and the monitoring of the log file continued too). When the process finishes I want the rest of the script to run.

I've searched a lot and I've tried a lot of things with grep and tail, and if, while, pipes etc, but I simply can't get it working.

What is the proper way?

Here's what I basically want, in an imaginary language (not bash/shell):

:START
RUN error_prone_process
IF "Error text" ~/logfile.txt
   KILL error_prone_process
   GOTO START

2 Answers 2

0

Your 'If' Command is checking to see if literally the words "Error text" is the logfile? Not entirely sure the attempt there though... I would use regex:

if grep -x 'ErrorText' "$File"; then
   KILL error_prone_process
   GOTO START
fi
2
  • GOTO doesn't exist in bash, that was just an example of something I don't know how to do properly (to make the code block repeat itself). Grep is also not enough, as that would run grep on the whole log file, not just the latest lines. Tail and grep combined may work, but I have no idea how I would implement that in a self-repeating code block. Regex is also unnecessary, just a regular grep would suffice for the text-matching part. Thanks for the answer nevertheless.
    – NewsGuy
    May 25, 2016 at 20:16
  • Can you edit your original post to include your actual code attempt and perhaps a snippit from your error log? It is rather ambiguous and you want a specific answer. Are you custom writing this logfile? If so, if you add line numbering could make it simple
    – Antony T.
    May 27, 2016 at 12:57
0

If you have access to the system configuration, consider using monit. To monitor logs, I have monit execute a script that will check the log and write OK or ERROR in another file. I then check that file for file length and freshness.

1
  • Thanks, but I want this script to be relatively portable. Simple stuff like this shouldn't require an external dependancy. I don't want to monitor the log file 24/7, just when the script is running.
    – NewsGuy
    May 26, 2016 at 7:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .