Search code examples
pythonpython-2.7processbackground-process

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.


Solution

  • 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)