Restarting a program after exception

前端 未结 4 758
执念已碎
执念已碎 2020-12-28 08:45

I have a program that queries an API every few seconds. Each response triggers a few functions which themselves make some calls to websites and such -- calls that I don\'t w

4条回答
  •  鱼传尺愫
    2020-12-28 09:24

    First make two files.

    One file called run.py and one called forever.py and put them in the same folder.

    Go to your terminal within that folder and type chmod +x forever.py

    run.py

    whatever code you want to run
    

    forever.py

    #!/usr/local/lib/python3.7
    from subprocess import Popen
    import sys
    
    filename = sys.argv[1]
    while True:
        print("\nStarting " + filename)
        p = Popen("python3 " + filename, shell=True)
        p.wait()
    

    Open a terminal window from the folder and type this:

    python3 ./forever.py retweet.py

    to start run.py and if it fails or has an exception, it'll just start over again.

    You now have a template to make sure if a file crashes or has an exception, you can restart it without being around. If this helps you, please give me a vote!

提交回复
热议问题