Answer the question
In order to leave comments, you need to log in
How to make a script start and stop function?
Hello! I am writing a telegram bot. There was such a need to both start the bot and turn it off. That is, I write / start - starts, / stop - stops. And such an idea arose. There will be only 2 scripts, one will receive commands and different messages, and the second script will be launched depending on the incoming command. That is, I write / start in the bot, the first script listens to this - it receives the / start command, then a couple more messages and the second script is launched. Then, if you need to stop it, then I write / stop, the first script receives this command and stops the second script. Maybe the idea is nonsense, but how to implement it? How to run scripts from other files (scripts)? In python
Answer the question
In order to leave comments, you need to log in
The first question is why?
However,
@bot.message_handler(commands=['start', 'stop'])
def control(message):
if message.text == '/start':
proc = subprocess.Popen(['python', 'second.py'], creationflags=subprocess.CREATE_NEW_CONSOLE)
with open('pid.txt', 'w') as f:
f.write(str(proc.pid))
elif message.text == '/stop':
with open('pid.txt', 'r') as f:
script_pid = int(f.read())
os.kill(script_pid, 9)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question