Making sure a Python script with subprocesses dies on SIGINT

前端 未结 5 907
感动是毒
感动是毒 2020-12-29 18:36

I\'ve got a command that I\'m wrapping in script and spawning from a Python script using subprocess.Popen. I\'m trying to make sure it dies if the

5条回答
  •  情深已故
    2020-12-29 19:07

    This hack will work, but it's ugly...

    Change the command to this:

    success_flag = '/tmp/success.flag'
    cmd = [ 'script', '-q', '-c', "sleep 2 && touch " + success_flag, '/dev/null']
    

    And put

    if os.path.isfile( success_flag ) :
        os.remove( success_flag )
    else :
        return
    

    at the end of the for loop

提交回复
热议问题