Python timing kill off

-sharp!/usr/bin/python3
import sys
import datetime
import os
import time
if __name__ == "__main__":
    while 1:
        startTime=datetime.datetime.now()
        main_path="./main.py"
        py=sys.executable
        status=os.system(r"{} {} {} {}".format(py, main_path, sys.argv[1],sys.argv[2]))
        if status==0:
            time.sleep(120)
        else:
            endTime=datetime.datetime.now()
            if endTime-startTime>60*60:
                break

I now start a script to determine that kill falls after an hour in the supermarket. Am I writing this correctly?

Mar.03,2022

  1. if status always returns 0, the program will not stop
  2. if hang stops when status=os.system (r'{} {} '.format (py, main_path, sys.argv [1], sys.argv [2])) is executed, the program will not stop
  3. .

if the requirement is only to end the script at a fixed time, then you can do it with a timer, demo as follows

-sharp!/usr/bin/env python
-sharp -*- coding: utf-8 -*-
import os
import time
import threading

def kill_me():
    os._exit(1)

if __name__ == '__main__':
    -sharp 3 
    timer = threading.Timer(3, kill_me)
    timer.start()

    while 1:
        print('xxxx')
        time.sleep(1)
Menu