Pour stopper une commande lancée avec subprocess sous python on peut utiliser le module time, exemple stopper un script python après 5 secondes:
import subprocess
import time
command = 'python my_script.py'
process = subprocess.Popen(command, shell=True)
time.sleep(5)
process.terminate()
autre exemple avec la commande unix find:
import subprocess
import time
command = 'find ./foo -perm 777'
process = subprocess.Popen(command, shell=True)
time.sleep(5)
process.terminate()
Autre méthode, en utilisant la fonction kill du module os:
import subprocess
import time
import os
import signal
command = 'find ./foo -perm 777'
process = subprocess.Popen(command, shell=True)
time.sleep(5)
os.kill(process.pid, signal.SIGINT)
Références
Liens | Site |
---|---|
time | python doc |
os.kill | python doc |
subprocess | doc python |
Kill a running subprocess call | stackoverflow |