![]() ![]() ![]() In my example I have used some variables just to made the configuration of the event handler a little bit easier to be undestood. Import some stuffģ ignore_patterns = None 4 ignore_directories = False 5 case_sensitive = True 6 my_event_handler = PatternMatchingEventHandler(patterns, ignore_patterns, ignore_directories, case_sensitive) Ok, let’s start and pretend that we want to create a program that logs all the file that are created or modified in the same directory of our program. I mean, we still need to code the program that will actually use this module, but trust me, that will be really easy! Now, create your virtual environment (optional but raccomended… at least by me), activate it and install the package watchdog with the following command: pip install watchdogĭone, we have almost finished, haven’t we? If you want to find out more about virtual environments (that’s probabilly because you haven’t read all my previous post, so shame on you!), just have a look at this article. As always, I raccomand to use virtual environments instead of installing packages system wide. To code this program we will need an additional module called “watchdog” (wow, who could have guessed it?) written by Yesudeep Mangalapilly, so let’s start by installing it. So, today we will code a watchdog in Python. Your program could set a watchdog to monitor that file and if the configuration file is modified you could think to reload it and apply the new configuration at runtime, without the need of restarting your program. When a change occurs, the watchdog report it to us raising a specific event that we can handle.įor example, let’s suppose you have developed a program that use a configuration file. But what is a “watchdog”?Ī watchdog is a little piece of software that monitors our filesystem looking for any changes (like the creation, change or deletion of a file or of a directory). Also, we’ll run xmessage in the background, so the function doesn’t block.Hey guys, today’s post is about how to create a watchdog in Python. The first one is the directory, and the other is the file that was removed. ![]() Integrating the inotifywait Output With Another Script Let’s see the new output: main/1/2/ MODIFY file1 Now, let’s write to the main/1/2/file1 file and then create a new empty file called file2 inside the main/1/2 directory: $ echo example2 > main/1/2/file1 Then, let’s monitor only for create and modify events: $ inotifywait -m -r -e create,modify main First, let’s finish the previous inotifywait command with Control+C. To do this, we’ll use the -e parameter and add the desired events separated by commas. Finally, let’s see how we can specify the exact events we want to monitor. We can notice that inotifywait automatically watches for events in the new directory. Let’s see the inotifywait output: main/2/ CREATE,ISDIR 1 Then, we’ll create a new file called file1 inside the new directory: Now, let’s create a new folder called 1 inside the main/2 directory. First, the base directory, then the event, and finally, the file that triggered that event. We can see that the inotifywait output has three columns. Let’s return to the shell where we run inotifywait to see its output: main/1/2/ CREATE file1 This command creates a new file inside the main/1/2 folder. Now, while inotifywait is still running, let’s open a new shell and run echo example > main/1/2/file1. With that, the inotifywait command will continue to run in the foreground, waiting for events. Beware: since -r was given, this may take a while! Let’s see how to monitor for any event in our main directory: $ inotifywait -m -r main Otherwise, inotifywait exits after the first event. This configures inotifywait to keep watching the directory forever. To monitor a directory tree, including its subdirectories, we’ll use the -r parameter. We can use the inotifywait command to monitor a directory tree.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |