T
T
TRXTSSA2021-11-21 17:31:18
Python
TRXTSSA, 2021-11-21 17:31:18

How to restart a file after an error?

try/except (and finally) won't work because the error occurs in the except block. It is necessary that the file (or code) is completely restarted after an error. I heard something about systemd and restart: on-failure in it, but did not understand how to use this thing.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-11-21
@TRXTSSA

Based on systemd, are you on Linux? So you have os.system (command) will return the exit code, which is equal to zero on normal completion.

import os
 
while True:
    exit_code = os.system("python3 script.py")  # Возможно тут следует написать полные пути
    if exit_code == 0:
        print("success!")
        break
    print(f"exit_code is {exit_code}, will retry")

And systemd is closer to the administration of the whole system. These are background services and tasks, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question