2

I have a python script that automatically ends after X amount of runs, at the start of the program it sets the value of restart.txt to 0 using this code.

restart = open("restart.txt", "w")
restart.write("0")
restart.close()

Then at the end of the program it resets the file to 1 so it can restart using inotify, like this.

restart = open("restart.txt", "w")
restart.write("1")
restart.close()
exit //closes program

I want an inotify script or something like that, that when the value of restart.txt changes to 1, to open the program again, a restart.

How would I go about doing that with an inotify script or something similar?

bertieb
  • 7,543
Jaf
  • 23

1 Answers1

1

The following snippet of bash code can be placed inside an executable bash script:

     FORMAT=$(echo -e "\033[1;33m%w%f\033[0m written")
     while /usr/bin/inotifywait -qe close_write --format "$FORMAT" restart.txt
     do
             [ $(cat restart.txt) -eq 1] && /path/to/program/to/be/executed || echo "nothing to do"
     done

The call to inotifywait watches the restart.txt file, in the quiet mode (i.e., suppressing some output) for an event (-e) which is close the file after having opened it to write. However, inotifywait cannot distinguish whether the file was really written to, so the following line implements a test: if restart.txt contains 1, then execute some file, but if still contains 0 then do nothing.

MariusMatutiae
  • 48,517
  • 12
  • 86
  • 136