Forums

Always On Task best approach?

Hi, I'm pretty new here , I have 4 telegram bots that are currently running on "Always on Tasks" I'm just wondering if this is the best and cheapest way to run them.

the bots only do something when something happens so not sure it needs to be on all the time. however I don't want to just run from the console as it gets shutdown after a day or so.

e.g 1 bot is an autoforward which forwards messages and images 1 bot is a troubleshooting guide so responds to input from a user. 1 is a bot for me to access my sqlite database

appreciate any advice

Thanks

If you're looking to reduce cost, is it possible for you to run your 4 bots in the same always on task?

Hi that does sound a better way to do it, however any idea how I would go about that?

would I need to combine all my 4 scripts into one? or somehow create one script that runs them all. sorry if these are daft questions

Yes create a single script which calls out to your other bots. Then run this script in your always on task.

That's exactly what I need! I will look into a way to do this, thanks very much!

Glad to hear it

managed to sort it thanks for your help this is the script i went with in case anyone else needs it :

import multiprocessing
import subprocess

# Define the paths to your bot scripts
SCRIPT_PATHS = [
    "SCRIPT 1 LOCATION",
    "SCRIPT 2 LOCATION",
    "SCRIPT 3 LOCATION",
    "SCRIPT 4 LOCATION"
]

# Define a function to start each bot script
def start_script(script_path):
    subprocess.run(["python3.10", script_path])

if __name__ == "__main__":
    # Create a separate process for each bot script
    processes = []
    for script_path in SCRIPT_PATHS:
        p = multiprocessing.Process(target=start_script, args=(script_path,))
        processes.append(p)
        p.start()

    # Wait for all processes to finish
    for p in processes:
        p.join()

Excellent, glad to hear you worked out a solution!