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,344
  • 36
  • 42
  • 54
Jaf
  • 23
  • 4
  • I may have misunderstood, but it seems like you want to immediately restart your script after it has been run. If this is the case, why not modify the script to repeat itself, or is a wrapper around the whole thing? Why are you interested in `inotify` ? – bertieb Sep 14 '15 at 09:55
  • @bertieb the purpose of my script is complete a certain action, 19 times then close itself. I want it to restart from within the script but one of the libaries in use does not support this. My alternative solution is to change a outside file and use inotify to restart when it changes. – Jaf Sep 14 '15 at 09:56
  • Which library, out of completeness' (and curiosity) sake? In any case, can you wrap by doing eg `while true; do python myscript.py; done` to repeatedly, endlessly invoke the python script? There may be a good reason to use `inotify` which I'm not seeing here but I'm a fan of keeping things simple! – bertieb Sep 14 '15 at 09:59
  • _Skype4Py_ and _cleverbot_ after a minimum of 19 lines to 30, it will start spewing ads into the skype conversation, if i restart it, it will never be able to spew an ad. The script uses a function for each purpose (reading and responding to messages) so it cannot be run inside a loop as you suggested. – Jaf Sep 14 '15 at 10:02
  • Okay, fair enough. I'm not seeing why it can't be run in a bash `while` loop- it is running, doing stuff, `exit`ing and starting again. Why is starting via an `inotify script` different from starting a fresh instance via `python myscript.py` ? It may be worthwhile including more context in your question to explain why this isn't a possibility; but I do accept that I might be missing something obvious! – bertieb Sep 14 '15 at 10:07
  • inotify is just one option, I am new to linux and am not very comfortable with it outside of basic tasks like starting a script. I would be open to other methods. – Jaf Sep 14 '15 at 10:10
  • Fair enough- for what you've written (`start A; A does stuff; exit; start A...`) I'd say that `inotify` is overkill. If the situation is different (eg signalling to restart `A` from another program `B`) it *might* be useful to do that via `inotify`, but as you say TMTOWTDI! – bertieb Sep 14 '15 at 10:14
  • Sure, How would i go about making a bash script to run it in a loop? – Jaf Sep 14 '15 at 10:16
  • As per my comment above: create file (named something useful like `doskype.sh`) with the contents `while true; do python ; done`, replacing `` with the actual script. Make it executable (`chmod +x doskype.sh`), then run via `./doskype.sh`. Incidentally, the Linux Documentation Project has a [Bash for Beginners guide](http://www.tldp.org/LDP/Bash-Beginners-Guide/html/). – bertieb Sep 14 '15 at 10:20

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
  • 46,990
  • 12
  • 80
  • 129