I need your help.

Dear All,

I need Your help....

we have a an out file that is updated from time to time ( every x seconds )... can we make a script to check if the file contains a spesific patteren.... if it does, we need to make an action like running another script.....

is this possible without makin cronjob to check wiehther the file contains the pattern or not ? I mean when the input file recieves "?????" do action.

Thanks so much for your help..

Did you mean to check pattern in the entire file each time ?

<grep should do this>

Or, check for the existence of the pattern in every data segment that is updated in 'x' seconds.

<Save, the file pointer and grep for the update section only>

Yes, it is possible but what's wrong with cron jobs ?

I know grep should do it.... but if I want to check the pattern every five seconds... I need to make a cronjob to run the grep command.... right ?
what Im asking is that if I can check it automatically once the file contains this pattern with out any cronjob...

once the file has the patteren..... do something...

I cant use cronjob becasue it's not allowed on our server :frowning:

Insted of every five seconds how about when it happens - see if
tail -f <filename>
meets your needs to keep getting newly added records.

Then you should be going for your own job, with an "always running script" to check for every 5 minutes or so

Dear matrixmadhan

Honestly... this is what we are trying not to do.... we cant keep checking the file because this will take a lot of time.... we need somekind of alert when it happens... what do you suggest... ?

Thnaks a lot

Sorry, if that was such a bad reply.

Truly, the problem in statement is "check for every 'n' units of time". If it has to be checked for 'n' units of time then for the each slice of time the file has to be definitely opened, parsed through, validated or checked against and then closed again.

This has to be definitely done by a process. Using crond for that is ruled out ( as said by you ). So, another process that does a minimal of crond ( a real minimal ) is needed.

Whats wrong with the suggestion I had posted ? For every 'n' units of time, the process is going to wake up and going to do its job; rest of the time it would be in sleep mode so its not blocking the run queue of the runnables nor using up a considerable time slice in doing not-so useful while (1) or something like that.

Any, event tracker or event notifier has to work with the resource ( open, read, close ) and then inform the process blocking on the event notifiers/trackers.

Or, did you mean something else ?

Experts here might post different and better solutions :slight_smile:

Why this would take a lot of time ?
A slot in the process table and a SIGALRM call for every 'n' units of time.

At the end, somebody has to alert that.

If you grep the whole file every time, you will find previous occurrences. If it is a huge logfile, for example, grepping thru the whole thing could take long than five seconds. It sounds exactly like monitoring a log file to me. Again, this is why we have tail -f.

Do you have anyway to get the process to do a log rotate - like signalling the process with a SIGHUP? This would make a full file grep feasible.

I was thinking in the following lines

  • open the file only once
  • sleep for 'n' units of time
  • once process is awake, store the current file position
  • reopen the file and position the file pointer to the value stored in previous step
  • now validate, grep, processing etc
  • sleep once again
  • just loop through the above process

Thank you both for your answers..

The problem is that Im not expert in UNIX... what I want is to trigger the script or alert or anything once the input file contains that patteren.. I dont want to make a cron job to check the file... I want this pattern to be like a trigger....

I know you are pulling out your hair by now :frowning: ...

Thanks againnnnn

That again boils down to reading the file "to know whether the pattern has occurred or not" which has to be done by some process.

How would you expect to automatically be identified about a pattern in a file without actually reading that ? This is as good as - not reading the file and arriving at an answer - "no such pattern in the file".

:slight_smile:

Ya I know I have to read it ..... but I thought that maybe there is a proccess to get rid of reading the file, and use the patteren as trigger .... :smiley:

Thanks a lot...

:b: matrixmadhan & jim mcnamara :b:

It can be done without reading the file if that is a requirement.

You should tell more about what this log file is, what process(es) are writing to it, at what rate and what Solaris release you are using.

Interesting ! :confused:

How is that possible to know a pattern in a file without actually reading it ?

I think this is not possible.

Did you mean, hiding or encapsulating the reading interface ?

I hope you'll agree the process which wrote it successfully knows it is there without needing to read the output file.

Yes, thats right. But in the most of the cases the systems are loosely coupled by design such that each of the unit performs their job so seldom you could control the functionality or add functionality to process that is creating the log messages.

For example:

Its not obvious that I request DW team to WebServer team to redirect all <example_logs> to be suppressed or erase. It is upto the reporting team to filter that out from the apache/website logs to not use them.

That is the reason, I didnt give out that possibility.

In any case, I don't mean to deny what you had said. I just assumed ( over - assumed ) thats not going to be the case.

:slight_smile:

I think it needs to be said again: tail -f sounds like the ticket. You can combine the tail -f and the grep in a simple Perl script. Whatever the implementation, the idea is to keep the file open, and attempt to read another line, say, once per second, or once every five seconds. If the read fails, it means no data has been appended to the file. If it succeeds, there is new data; read and examine, and if it's a hit, take some action (terminate? send another process a signal? send an email?)

Please could you explain a little more :confused: .... Im not fimailiar with tail -f command.... what does it do exactly ?

Thanks