0

How to terminate another script within a script

Say I have two scripts, main.py, and second.py. Is there a way of terminating second.py IN main.py? Like a quit() function or something? See the link for more details about this example: https://code.sololearn.com/cpxA5ef5KvbP (Use the link only for more info about what I am trying to do, running it isn't going to work)

11th Sep 2021, 3:59 PM
Ethan
Ethan - avatar
4 Respuestas
+ 2
There are sys library that exit the actual running process but you can use a simple return statement that will exit from current function.
11th Sep 2021, 4:52 PM
HBhZ_C
HBhZ_C - avatar
+ 2
Ethan the only way i can imagine doing this is having something in queue, memory, or database keeping track of the running script with a terminate flag. the script would be continually checking the flag and when someone or a script that flips the flag, it will then end the script. i would be interested if someone is able to come up with a solution you are looking for.
12th Sep 2021, 12:21 AM
you are smart. you are brave.
you are smart. you are brave. - avatar
+ 1
That's right, using sys.exit() can work. However after some testing I realised that to interrupt a constantly running script you have to import subprocess and kill the script with: subprocess.Popen(["python", "second.py"]).kill() Your answer will work with a function that's only called one time, if it's a constantly running script, one should use the subprocess method. Thanks for the answer anyway!
11th Sep 2021, 5:08 PM
Ethan
Ethan - avatar
+ 1
for constant running script you can use cron for Linux and Schedule with Time libraries for windows, here's an example of mine combined with subprocess https://github.com/IT-Support-L2/Automated_PowerShell_Flush_DNS_with_Python/blob/master/automated_task.py
11th Sep 2021, 5:45 PM
iTech
iTech - avatar