Search code examples
pythonpython-2.7cmdlogmein

Taskkill through LogMeIn


I am using a LogMeIn OneToMany task to deploy an update to some PCs running my application. I have a python process that spawns a child .exe. It runs as "python.exe" (what the task list says) and has a title of "Administrator: AppRunner".

I can't kill python.exe because there are other scripts running that I don't want to die. So, I try to kill based on the title name. The below works locally but does not work when executing through logmein:

os.system('taskkill /f /t /fi "WindowTitle eq Administrator:  AppRunner"')

I have other taskkills that kill other executables and they work fine e.g.:

os.system('taskkill /f /im program.exe')

Is there anything obvious that I am missing, or can someone help me with a method to debug something like this so that I can maybe get a hook into the problem?


Solution

  • As you have noted yourself, from the Microsoft help pages for taskkill:

    The WINDOWTITLE and STATUS filters are not supported when a remote system is specified.

    Instead, you could get your child process to write a PIDfile:

    import os
    
    pid = os.getpid()
    with open('c:\\temp\\myapp.pid','w') as pidfile:
        pidfile.write(str(pid))
    

    And then when you want to kill the process, get the PID from the file and kill it using /PID switch on taskkill. Assuming you've read the PID into a variable mypid:

    os.system('taskkill /PID {} /f'.format(mypid))