Check START and END strings on the last minute comparing with current time
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.
Check START and END strings on the last minute comparing with current time
I have a log file.
This log file has following format.
Log file is placed on dynamic a path, so first I need to find most recent log file on some folder and after that I need to know if there is a START and END string on the last log file that is being written on the last minute related to last record of that log file.
Haven't started yet. I shouldn't use tail since it always should be there waiting for the log to be written but first I need to solve how find for the last log file inside that folder.
Here's a blog regarding BASH scripting; there are other languages for scripting, but I lean towards BASH myself. There are also various guides about BASH scripting just by searching the web.
The main point about BASH scripting which I emphasize is that "anything which you can do on a command line, you can script", which is to say that in order to accomplish your intended goal, your first actions are "typing commands manually and observing the outcome".
Therefore you can see what files are newest, by sorting by date, and see whether or not a file meets your START/END criteria, by visual inspection.
For a script you can also list files by date, and then look at the newest log file and search for both text terms and get a result.
A suggestion is to attempt to write a script where you first find the file you should be checking and then figure out how to search the file for both words, for example two different grep commands much like you would do from the command line. I don't believe you stated what you intend to do beyond the point of discovery that a log is complete, but at that point you can perform other intended actions.
My simplest actions here would be to perform an 'ls -lrt' or some other form of ls to find the most recently created/modified log file. I would save that file name as a variable in my script and I would then search that file; using grep for the string START and the string END, if both of those tests succeeded, then that would tell me that my found file was one that I wanted. I would probably do this for all files anyways and turn on debug or echo my results to a log file so that I could understand how my script really worked, before I then limited it to operate as I intended.
Here's a blog regarding BASH scripting; there are other languages for scripting, but I lean towards BASH myself. There are also various guides about BASH scripting just by searching the web.
The main point about BASH scripting which I emphasize is that "anything which you can do on a command line, you can script", which is to say that in order to accomplish your intended goal, your first actions are "typing commands manually and observing the outcome".
Therefore you can see what files are newest, by sorting by date, and see whether or not a file meets your START/END criteria, by visual inspection.
For a script you can also list files by date, and then look at the newest log file and search for both text terms and get a result.
A suggestion is to attempt to write a script where you first find the file you should be checking and then figure out how to search the file for both words, for example two different grep commands much like you would do from the command line. I don't believe you stated what you intend to do beyond the point of discovery that a log is complete, but at that point you can perform other intended actions.
My simplest actions here would be to perform an 'ls -lrt' or some other form of ls to find the most recently created/modified log file. I would save that file name as a variable in my script and I would then search that file; using grep for the string START and the string END, if both of those tests succeeded, then that would tell me that my found file was one that I wanted. I would probably do this for all files anyways and turn on debug or echo my results to a log file so that I could understand how my script really worked, before I then limited it to operate as I intended.
Thanks for your time to answer rtmistler, but is it possible to go to the practice? Besides, you don't mention about how you would treat the time if it's needed to know if the log got the START and END strings in the last minute. If you doesnt mind, could you please help me building this script in bash?
I think you need to be more clear about what your intended actions need to be. Please do not merely reiterate your first post, because that's already visible. You have log files, you need to determine which is the newest log file and you need to determine that the most recent log file contains both START and END records. It seems as if you wish to check these once per minute to see if changes meet your criteria. That's about all I can determine. Perhaps what you're saying is that you need to check a log file once per minute to determine that the END log record has been added, or perhaps you need to determine cases where the START and END log entries are greater than one minute apart, because the example you've shown illustrates all entries are at the same time versus separated in time. I don't know the full requirements.
I'd say start by writing out the command prompt actions which you do to determine what you need to determine. Once you know those, you can write a script and further vary things to automate your actions.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.