'Watch changes in a file and restart python script

I want to restart a python script(restart.py) whenever some edits a file (test.txt)

=====restart.py====

from time import sleep
for i in range (1000):
    print (i)
    sleep(2)

Then Im watching for changes using this command

 inotify-hookable -f test.txt  -c "sh kill.sh"

====kill.sh=====

#!/bin/sh
ps -ef | grep restart | grep -v grep | awk '{print $2}' | xargs kill
/usr/local/bin/python3.7 restart.py

Now whats happening is that the very first time I edit test.txt, it kills the restart.py script and starts it again. But after that if i make any changes , it doesnt kill and start the script again. So in a way inotify works for only first edit of the test.txt file and never again. How can I make it execute "kill & restart" whenever there is an edit in test.txt file ?

─╯
Fri Apr 29 01:58:15 2022 : Starting up, watching files <test>
Fri Apr 29 01:58:15 2022 : FINISHED setting up watches. Took 0.00s with 1 watches added, 0 removed, 0 replaced. Have 1 total watches
Fri Apr 29 01:58:35 2022 : Had changes in your paths
Fri Apr 29 01:58:35 2022 : Running global hooks
Fri Apr 29 01:58:35 2022 : Running <sh kill.sh>
0
1
2
3
4
5

----------------------TRIAL 2-----------------

Here i tried this python script with watchdog, but even this is not working. Works for the first time I edit the test.txt file, thereafter it doesnt restart/kill the hello.py script at all

import time
from threading import Timer
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import os,signal
from time import sleep
import icecream as ic

def kill_process(name):
    #pid_=999999999
    for line in os.popen("ps ax | grep " + name + " | grep -v grep"):
        fields = line.split()
        print (fields[0])
        pid = fields[0]
        print ("PID==",pid)

    ic(pid)
    return pid

class  MyHandler(FileSystemEventHandler):
    def  on_modified(self,  event):
         if len(event.src_path)>0:
             print(f'event type: {event.event_type} path : {event.src_path}')

             try:
                 pd=kill_process("hello.py")
                 os.kill(int(pd), signal.SIGKILL)
             except:
                 os.system("python3.7 hello.py 1")


if __name__ ==  "__main__":
    event_handler = MyHandler()
    observer = Observer()
    observer.schedule(event_handler,  path='test.txt',  recursive=False)
    observer.start()
    try:
        while  True:
            time.sleep(1)
    except  KeyboardInterrupt:
        observer.stop()
    observer.join()

╰─❯ cat hello.py ─╯

from time import sleep
for i in range (1000):
    print (i)
    sleep(10)


Solution 1:[1]

you need to launch a finishing command

You can solve your issue by adding a & on restart launch

#!/bin/sh
ps -ef | grep restart | grep -v grep | awk '{print $2}' | xargs kill
/usr/local/bin/python3.7 restart.py&

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ôrel