1

I have a script in which I use subprocess.Popen to start an external program and process.kill() to kill it pretty much as soon as it's started. I've been getting Windows Error [5] (Access Denied) every time the script tries to kill it. I've realized that the pid of the program is actually changing after it's opened. Is there a way, in Python, to monitor the process for the change, or to just retrieve the new pid?

Here is the code:

import subprocess
import time

proc  =  subprocess.Popen(Path/to/WinSCP.exe)
time.sleep(2)
proc.kill()

The error:

Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "C:\Path", line 231, in __call__
self.scpsetup()
File "C:\Path", line 329, in scpsetup
proc.kill()
File "C:\Python27\lib\subprocess.py", line 1019, in terminate
_subprocess.TerminateProcess(self._handle, 1)
WindowsError: [Error 5] Access is denied
Jason
  • 31
  • 5
  • 2
    The process cannot change its PID. – wRAR Mar 25 '13 at 18:20
  • 1
    look into http://amoffat.github.com/sh . that's the way i recomend to use subprocesses in python. Other way you could parse the bash response of ps -aux | grep 'process_name' – mihaicc Mar 25 '13 at 18:21
  • Is the external program you start something well known you can tell us? It sounds as if your started process starts another process. @viper33m The `Windows Error [5]` sound like this error occurs under Windows, which would rule `ps` and `grep` out. – BergmannF Mar 25 '13 at 18:22
  • 1
    @Jason Show the *actual code* and error you are getting! It's a waste of time to make us guess when you did *not* give us a [SSCCE](http://sscce.org/) that shows the problem. By the way, the Access Denied error does not fit with your descript. I think you simply do not have the rights to kill the process. – Bakuriu Mar 25 '13 at 20:02
  • 1
    @Bakuriu I definitely have the rights. I can kill it if I don't wait, but in that case the program isn't running long enough to do what it has to do. When I wait the problem is that the pid is different from what proc.pid has stored. In fact the pid that proc has isn't even showing up as a process and WinSCP's pid is totally different. – Jason Mar 25 '13 at 20:28
  • 1
    @Jason Did you check [this](http://superuser.com/questions/109010/kill-a-process-which-gives-access-denied) and [this](http://stackoverflow.com/questions/2868129/terminate-subprocess-in-windows-access-denied) related questions? – Bakuriu Mar 25 '13 at 22:09
  • 1
    @Bakuriu I had a look and the second one matched the problem. Because of that I revisited that option and found that subprocess has more options than other ways of sending commands directly. So I was able to redirect the output and get the result I was looking for. Thanks everybody for your help. If you come up with any prettier ways of doing this please do let me know. – Jason Mar 26 '13 at 14:20

1 Answers1

2

This is what I ended up doing;

import tempfile
import subprocess
import time

# Create a temp file to receive output
tf      =  tempfile.NamedTemporaryFile(delete=False)
output  =  open(tf.name, "w")

# Open and close WinSCP
subprocess.Popen(Path/To/WinSCP.exe)
time.sleep(2)
subprocess.call("TASKKILL /IM WinSCP.exe", stdout=output)
tf.close()

The issue I had with methods like this before was that I couldn't hide the output of the command. This may not be the prettiest way to accomplish this but it works.

Also note that I am using Windows 8. I understand that the command itself may vary slightly in different versions of Windows.

Jason
  • 31
  • 5