We often need to run Python scripts in the background to monitor the system or do other things; But due to various problems, it’s not the Python script code; The script will hang during execution. Manual restart is not practical, spend every day waiting for the restart…

So let’s write a shell script that controls python to restart automatically when it dies:

The shell script is as follows:

We then use the shell script to start the Python program:

In the background, kill -9 to kill the Python process. See the Python script restarts automatically