Skip to content

Instantly share code, notes, and snippets.

@chenzx
Created June 25, 2013 15:56
Show Gist options
  • Select an option

  • Save chenzx/5859649 to your computer and use it in GitHub Desktop.

Select an option

Save chenzx/5859649 to your computer and use it in GitHub Desktop.
A Python 2.7 script to keep the win32 app running, I use it to monitor my Thunder downloader, since it is keeping crashing due to insufficient disk space
import os
import sys
import time
PROCESS_NAME = u"Thunder.exe"
PROCESS_STARTUP_CMDLINE = 'E:\ThunderPortable\ThunderPortable.exe'
def win32app_not_running():
from win32com.client import GetObject
#Need download http://sourceforge.net/projects/pywin32/files/pywin32/Build%20218/pywin32-218.win32-py2.7.exe/download
WMI = GetObject('winmgmts:')
processes = WMI.InstancesOf('Win32_Process')
# p = WMI.ExecQuery('select * from Win32_Process where Name="Thunder.exe"')
processNames = [process.Properties_('Name').Value for process in processes]
for processName in processNames:
if processName==PROCESS_NAME:
#print "%s is runing" % (PROCESS_NAME)
return False
return True
def start_the_win32app():
from subprocess import Popen
DETACHED_PROCESS = 0x00000008
cmd = [
PROCESS_STARTUP_CMDLINE
]
p = Popen(cmd,shell=False,stdin=None,stdout=None,stderr=None,close_fds=True,creationflags=DETACHED_PROCESS)
while True:
time.sleep(5)
if win32app_not_running():
start_the_win32app()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment