Trouble killing a process on windows

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Nelson

    #1

    Trouble killing a process on windows

    Hi, I'd like to start a program, run it for a while, then terminate
    it. I can do this on linux, but I'm new to working with windows.
    Here's my script:

    from subprocess import Popen
    from time import sleep
    import win32api
    war3game = Popen(["C:\Program Files\Warcraft III\Frozen Throne.exe"])
    sleep(30)
    print "slept for 30"
    print win32api.Termin ateProcess(int( war3game._handl e),-1)
    #print
    ctypes.windll.k ernel32.Termina teProcess(int(w ar3game._handle ),-1)
    print "terminated process"

    Here's the output:
    slept for 30
    Traceback (most recent call last):
    File "C:\Python24\wa rcraft\runwar3. py", line 7, in ?
    print win32api.Termin ateProcess(int( war3game._handl e),-1)
    error: (5, 'TerminateProce ss', 'Access is denied.')

    I'm logged in as adminstrator. Does anyone know how to fix this
    problem?
    Thanks for your time,
    Tom

  • Tim Golden

    #2
    Re: Trouble killing a process on windows

    Thomas Nelson wrote:
    from subprocess import Popen
    from time import sleep
    import win32api
    war3game = Popen(["C:\Program Files\Warcraft III\Frozen Throne.exe"])
    sleep(30)
    print "slept for 30"
    print win32api.Termin ateProcess(int( war3game._handl e),-1)
    #print
    ctypes.windll.k ernel32.Termina teProcess(int(w ar3game._handle ),-1)
    print "terminated process"
    >
    Here's the output:
    slept for 30
    Traceback (most recent call last):
    File "C:\Python24\wa rcraft\runwar3. py", line 7, in ?
    print win32api.Termin ateProcess(int( war3game._handl e),-1)
    error: (5, 'TerminateProce ss', 'Access is denied.')
    >
    I'm logged in as adminstrator. Does anyone know how to fix this
    problem?
    There's nothing obvious. I assume you got your info
    from here?



    Just for completeness, have you tried the pid-based technique
    shown there? I've no idea why it should work if this one
    doesn't but... (I have a slight suspicion that the fact that
    it opens the process with a "for-termination" flag might help).

    Failing that, you can see if WMI can do it (although I assume
    that, under the covers, WMI just calls TerminateProces s):



    I suppose you might have to adjust your token privs to include,
    say the Debug priv. This is designed to let you take control
    of any process (and terminate it, or whatever). If it looks
    like you need to do that, post back and I -- or someone else --
    can try to run up an example.

    TJG

    Comment

    • reed

      #3
      Re: Trouble killing a process on windows

      On Jun 2, 12:27 pm, Thomas Nelson <t...@mail.utex as.eduwrote:
      Hi, I'd like to start a program, run it for a while, then terminate
      it. I can do this on linux, but I'm new to working with windows.
      Here's my script:
      >
      from subprocess import Popen
      from time import sleep
      import win32api
      war3game = Popen(["C:\Program Files\Warcraft III\Frozen Throne.exe"])
      sleep(30)
      print "slept for 30"
      print win32api.Termin ateProcess(int( war3game._handl e),-1)
      #print
      ctypes.windll.k ernel32.Termina teProcess(int(w ar3game._handle ),-1)
      print "terminated process"
      >
      Here's the output:
      slept for 30
      Traceback (most recent call last):
      File "C:\Python24\wa rcraft\runwar3. py", line 7, in ?
      print win32api.Termin ateProcess(int( war3game._handl e),-1)
      error: (5, 'TerminateProce ss', 'Access is denied.')
      >
      I'm logged in as adminstrator. Does anyone know how to fix this
      problem?
      Thanks for your time,
      Tom
      kill = Popen(['taskkill', war3game.pid])

      *shrugs*

      maybe it is just taskill, or maybe you need the full path.
      don't recall as I quit using windows.

      Takskill on XP and newer maybe

      Comment

      • Thomas Nelson

        #4
        Re: Trouble killing a process on windows

        On Jun 2, 11:43 am, Tim Golden <m...@timgolden .me.ukwrote:
        Thomas Nelson wrote:
        from subprocess import Popen
        from time import sleep
        import win32api
        war3game = Popen(["C:\Program Files\Warcraft III\Frozen Throne.exe"])
        sleep(30)
        print "slept for 30"
        print win32api.Termin ateProcess(int( war3game._handl e),-1)
        #print
        ctypes.windll.k ernel32.Termina teProcess(int(w ar3game._handle ),-1)
        print "terminated process"
        >
        Here's the output:
        slept for 30
        Traceback (most recent call last):
        File "C:\Python24\wa rcraft\runwar3. py", line 7, in ?
        print win32api.Termin ateProcess(int( war3game._handl e),-1)
        error: (5, 'TerminateProce ss', 'Access is denied.')
        >
        I'm logged in as adminstrator. Does anyone know how to fix this
        problem?
        >
        There's nothing obvious. I assume you got your info
        from here?
        >

        >
        Just for completeness, have you tried the pid-based technique
        shown there? I've no idea why it should work if this one
        doesn't but... (I have a slight suspicion that the fact that
        it opens the process with a "for-termination" flag might help).
        >
        Failing that, you can see if WMI can do it (although I assume
        that, under the covers, WMI just calls TerminateProces s):
        >

        >
        I suppose you might have to adjust your token privs to include,
        say the Debug priv. This is designed to let you take control
        of any process (and terminate it, or whatever). If it looks
        like you need to do that, post back and I -- or someone else --
        can try to run up an example.
        >
        TJG
        I Tried the PID method, and the Taskkill method, and neither worked.
        This is what I get for trying to work on a windows machine. If you
        could explain how to add the debug privilege, that would be great.
        Just out of curiosity, is there a reason a python module like os
        couldn't have a universal terminateProces s type function, that works
        on all systems? Something like os.listdir seems to work really well
        everywhere. Maybe killing processes is too complicated for this?

        Thanks for the help,
        Tom

        Comment

        • Tim Golden

          #5
          Re: Trouble killing a process on windows

          Thomas Nelson wrote:

          [... re Access Denied error on trying to TerminateProces s ...]

          [Tim Golden]
          >I suppose you might have to adjust your token privs to include,
          >say the Debug priv. This is designed to let you take control
          >of any process (and terminate it, or whatever). If it looks
          >like you need to do that, post back and I -- or someone else --
          >can try to run up an example.
          [Thomas Nelson]
          I Tried the PID method, and the Taskkill method, and neither worked.
          This is what I get for trying to work on a windows machine. If you
          could explain how to add the debug privilege, that would be great.
          OK, this being Windows security, there's a couple
          of hoops to jump through, but nothing too bad.

          <code>
          import win32api
          import win32security as sec

          #
          # This function is merely window-dressing
          # to make it easier to see whether the
          # code has worked or not.
          #
          def priv_status (hToken, priv):
          for p, status in sec.GetTokenInf ormation (
          hToken,
          sec.TokenPrivil eges
          ):
          if p == priv:
          if status == 0:
          return "disabled"
          else:
          return "enabled"
          else:
          return "disabled"

          process = win32api.GetCur rentProcess ()
          token_access_fl ags = sec.TOKEN_ADJUS T_PRIVILEGES | sec.TOKEN_QUERY
          hToken = sec.OpenProcess Token (process, token_access_fl ags)
          debug_priv = sec.LookupPrivi legeValue (None, sec.SE_DEBUG_NA ME)

          print "Debug is", priv_status (hToken, debug_priv)

          privs = [(debug_priv, sec.SE_PRIVILEG E_ENABLED)]
          sec.AdjustToken Privileges (hToken, 0, privs)

          print "Debug is", priv_status (hToken, debug_priv)

          </code>
          Just out of curiosity, is there a reason a python module like os
          couldn't have a universal terminateProces s type function, that works
          on all systems? Something like os.listdir seems to work really well
          everywhere. Maybe killing processes is too complicated for this?
          Not to say that there could never be one, but I suspect
          that the difference of APis and the number of corner
          cases makes it a daunting task for maintenance. The
          code would have to work on every platform Python works
          on -- which is no small number -- and would have to be
          maintained across the many and varied changes on each
          of those platforms.

          Still, if you think you're in with a chance, go ahead
          and offer :)

          Let us know if the DEBUG priv thing works or not.

          TJG

          Comment

          Working...