How to create a service that watches a file creation and execute commands RRS feed

  • Question

  • Hi,

    I have a system which generates a log file that I want to monitor and keeps records of warnings and errors.

    - The logfile changes its name from "log-file.txt" to "log-file.txt1" when it reaches to its maximum size of 2MB.

    - It keeps up to 4 log files. txt, txt1, txt2, txt3

    I would like to acomplish:

    - monitor log-file.txt for issues happening currently and send notification

    - store all errors and warnings to a file

    I have a script to find warning and error records from the log file but not sure what would be the best way to monitor "log-file.txt" for current issues 24/7 as well as watch the creation of "log-file.txt1" to store all warnings and errors.

    Much appreciated for sharing any ideas.

    • Moved by jrv Friday, May 29, 2020 3:35 AM Abandoned
    Friday, May 29, 2020 12:07 AM

All replies

  • If you search with your search provider you will find many blogs that will tell you how to write a service.

    We cannot teach you programming.  That is beyond the scope of this forum.  Also read the following carefully.

    Please carefully review the following links to set your expectation for posting in technical forums.


    Friday, May 29, 2020 1:50 AM
  • Often, sharing codes narrows it to one way of solving it so I tried to hear how others would approach, but gave you the wrong idea. I thought I was doing "7. Describe the goal, not the attempted solution", from the link you shared. Sorry, been answering rather than asking, but learning now.

    Storing log:

    I tried file system watcher to hook creation of txt1 file so I can store the completed log file (txt is current and keeps changing) Similar to https://stackoverflow.com/questions/29066742/watch-file-for-changes-and-run-command-with-powershell It catches the file creation and works well when I run it under my login session. Somehow it does not work when I run this as scheduled task with system account at start up. Not sure if this is the best way but I get so much of this register-event method when I google, but seen replies that it is not the best way for running continuously. Just wondering if anyone tries similar in different ways. I am not asking for codes but asking forum to share what are the possible approaches that I can start searching more details for.

    Monitoring current log:

    For this, I am running out of idea. The current log file gets created less than 2 minutes when there is a lot to log or, longer than 30 minutes when there is not much issues. Because of the inconsistency, It seems difficult to catch all errors. if running a script to catch errors every 5 minutes, I will miss possibly 2 log files if there was an issue resulted in creating 2 log files in 4 minutes. If I catch when txt1 created, I will not have current log reported for 30 minutes sometimes.

    If this is still a bit beyond for the forum to answer, I will post my codes as some do. But I am seeking advice on how best to approach to accomplish my goal. I can write programs and have done many powershell scripting for sys admin tasks but lack of PowerShell module/library knowledge for this case. If an expert can share an idea, I am sure I can google to see how to use function and correct mistakes on my own.

    Friday, May 29, 2020 2:46 AM
  • You are asking us top design a solution for you.  This forum is for questions about a script you have written.  We cannot take a laundry list of requirements and deliver a solution even if you write it like a long discussion of your lack of knowledge.

    Ask a question and post an example of your script.


    Friday, May 29, 2020 2:54 AM
  • From your comment I can see what you mean by "beyond the forum to answer". I will delete this and come back with smaller questions to answer easily.

    #EDIT: I cannot seem to remove this post. Please remove this. Thanks.

    Friday, May 29, 2020 3:01 AM