-3

I'm running 6 python files on aws EC2 ubuntu instance. they are telegram bots. they runs fine, but once a while one of the files stops running. and I've to find the screen and run it again.

is there any way to keep this script running reliably?

if I reboot ubuntu using crontab every day, would it automatically run all the .py files afterwords?

Mantra
  • 11
  • 5
  • Look into running them as services instead of using `screen` https://stackoverflow.com/questions/16420092/how-to-make-python-script-run-as-service – Mark B Sep 11 '21 at 14:21

1 Answers1

0

You can make a shell script to check if a specific python file is running or not and if not start that file. Once that shell script is done and working, you can make a cron job for that shell script to check every x minutes or however you want.

Single Application Lookout

The following Bash script checks if a specified python script is running or not and if that is not running then it starts the python script

#!/bin/bash
current_service_name="YOU_SCRIPT_NAME.py"    
if ! pgrep -f "python3 ${current_service_name}"
then
    python3 ${current_service_name}
fi

Multiple Application Lookout

#!/bin/bash
all_services=("YOUR_SCRIPT_NAME_1.py" "YOUR_SCRIPT_NAME_2.py" "YOUR_SCRIPT_NAME_3.py" "YOUR_SCRIPT_NAME_4.py" "YOUR_SCRIPT_NAME_5.py" "YOUR_SCRIPT_NAME_6.py")  

for index in ${!all_services[@]} ; do
    current_service_name="${all_services[$index]}"
    if ! pgrep -f "python3 ${current_service_name}"
    then
        python3 ${current_service_name}
    fi
done
t.abraham
  • 108
  • 1
  • 9