Ad

Python 2.7 : Kill Last Background Process

I'm using subprocess.call(ping ... &, shell=True) to run a background process, and after 5 secondes, I want to kill this process. I'm trying with the Bash equivalent kill $! but it's not working.

Thanks for any help.

Ad

Answer

have you tried like this, try with subprocess.Popen :

import os
import signal
import time
import subprocess

process = subprocess.Popen("ssh [email protected] 'echo 'rootpass' | sudo -Sv && bash -s' < ../attaques/Ping_flood.sh &", shell=True)
time.sleep(5)
os.killpg(process.pid, signal.SIGTERM)
Ad
source: stackoverflow.com
Ad