Home > Enterprise >  How do I run a bash command (in this instance, python running a script) when a specific file changes
How do I run a bash command (in this instance, python running a script) when a specific file changes

Time:11-02

I have this .AppImage that, when it updates itself, changes its name to match its version. I also have a python script to update a .desktop shortcut with the new file name. Is there any way to detect the AppImages' name change, then run the python script?

I've tried to find a bash command that detects a file's name change, but have only come up with results on how to change a file's name.

CodePudding user response:

I think watchdog is what you're looking for: https://github.com/gorakhargosh/watchdog

Here's an example from the readme that checks every second for file changes.

import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    path = sys.argv[1] if len(sys.argv) > 1 else '.'
    event_handler = LoggingEventHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    finally:
        observer.stop()
        observer.join()

CodePudding user response:

You could use inotify-tools. Try this, in one terminal:

$ touch foo
$ inotifywait -e move_self foo 2>/dev/null | grep MOVE_SELF

Now in another terminal move foo, say mv foo bar. You will see foo MOVE_SELF in the first terminal

You could easily put this in a script. Maybe something like this:

inotifywait -e move_self /path/to/foobar.AppImage 2>/dev/null | \
    grep -q MOVE_SELF && \
    python /path/to/script.py
  • Related