-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?

1

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

I get this for one script. but how do I go about it for 6?
Hi I have added another answer which is applicable for multiple scripts
ah, got it. thanks. I thought of something like this but wasn't sure. thanks again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.