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)
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.
+ 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.
+ 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!
+ 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