Coder Social home page Coder Social logo

watcher's Introduction

Watcher

A simple python based daemon to react to file change events using user-defined actions. The actions are to be provided in a JSON file.

  • Works on Linux only (for now).

Prerequisites

Requires python3 and the pyinotify module which can be installed using

pip3 install pyinotify

Example

Running the script is as simple as typing

$ ./watcher.py

at the shell prompt. After the launch, a line like

watcher daemon is running. Delete pid file .watcher_2302527 to stop.

will be displayed in the terminal. At this point watcher has created a process id (pid) file in the directory, which you can use to kill the daemon simply by removing the file

$ rm .watcher_2302527

The contents of the pid file points to the .json file containing the commands/ actions as rules to be executed while responding to file change events. By default, watcher looks for a file named watcher_rules.json in the working directory. If it does not find the rules file, watcher will create it for you, which you can then populate using appropriate JSON syntax.

E.g., if you want to watch a file named hello.py for changes and then run it in the terminal when it gets modified, the JSON rules file will look like

{
	"hello.py" : { "cmds" : "python hello.py", "pause" : false}
}

The "pause" JSON-key is a boolean and, if needed, can be used to temporarily deactivate the execution of the commands listed under the field "cmds".

You can include additional commands under the "cmds" field as follows:

{
	"hello.py" : { "cmds" : ["python hello.py", "cat hello.py"], "pause" : false}
}

Watcher will execute the commands for you, one after another, when hello.py has been modified. Individual commands can be paused by adding the comment symbol # (if you are using bash) at the start of that specific command.

Also, you can add other files to be watched in the rules file as follows:

{
	"hello.py" : { "cmds" : ["python hello.py", "cat hello.py"], "pause" : false},
	"greetings.sh" : { "cmds" : "bash greetings.sh", "pause" : false}
}

In this case, the shell-script greetings.sh will be executed whenever it gets modified.

Logs

Watcher stores all relevant information, such as info, debug messages and warnings, in a log file created in the directory where watcher.py was launched. The default name for the log file is watcher.log and can be changed using the -log option (see next section). If the log file already exists watcher will append the new messages to the end of the file, preserving the previous log entries.

Extra options

The script watcher.py can take some additional command-line options to change its default behaviour:

  • -rules rules_file change the default JSON file containing the watch rules .
  • -log log_file change the default logfile
  • -h show the help message and exits without launching the daemon.

watcher's People

Contributors

arijit-hal avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.