-1

I am using a bash executable to run a python script in a loop

Sometimes the browser (run by the python script) will stop loading and stop working for hours until I manually come and close the window which will cause the executable to re-run the python script. How can I do this automatically? If no new line is shown in the terminal for 2 mins, then quit python code and run it again.

This is what I have right now:

while true ; do 
     python3 /Users/Name/Desktop/pythoncode.py
done

It will run the code in a loop. Now I want it to close the code and run it again if no line is outputted in the terminal for 2 mins (the python script shows its progress in the terminal)

Thanks and happy new year

  • https://stackoverflow.com/questions/60738749/timeout-a-bash-script-when-there-is-no-output-for-a-while this looks promising – Havihavi Dec 31 '20 at 01:34
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). Also see [How much research](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) and the [Question Checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). It appears that you didn't bother to search as much as "Python timeout", so this question is premature. – Prune Dec 31 '20 at 01:40
  • I did do lots of searching before posting this question. I believe you are misunderstanding the question. I do not want it to timeout after 2 min if pythoncode.py outputs to stdout. if it does it should run forever and not be timedout – Ardalan G. Jan 02 '21 at 10:09
  • @Prune I want to terminate a python script if and only if it doesn’t output anything to the terminal in 2 min. Else loop forever – Ardalan G. Jan 02 '21 at 10:11
  • I think you misunderstand the solution path. The timeout logic still applies. You have to set a process to detect output, and another to timeout after 2 minutes. If the first one receives any output, then it kills the timeout process. If the timeout process reaches its 2-minute limit, then it terminates the job. Your question doesn't show an attempt at either process, although you've described the functionality. – Prune Jan 02 '21 at 19:46

1 Answers1

-1

I think the problem should be handled in the pythoncode.py.You can detect whether there was a change in the last 2 minutes by using a setInterval method and increase a counter for every action that is taken by the code. If it hasn't change the last 2 minutes, you can close it. The other script will run it again.

Python Equivalent of setInterval()?

In the other python file which will run the script, you can use os.system()

Run python script only if it's not running

aligumustosun
  • 152
  • 10
  • What is this? A mixture of bash and python? Even if it is pseudocode, it still does not answer the question which wants to rerun the script only if there is no output for 2 minutes. – Countour-Integral Dec 31 '20 at 01:10
  • You are right, I didn't understand it properly. – aligumustosun Dec 31 '20 at 01:19
  • Thanks for your reply. I will try this. Sorry can't upvote since less than 15 reputation. Haha. Don't know why it has -1 upvotes. Seems very helpful. Thanks again : ) – Ardalan G. Dec 31 '20 at 01:49
  • In summary, I want to terminate a python script if it doesn’t output anything to the terminal in 2 min – Ardalan G. Dec 31 '20 at 02:01