Hila Hila - 2 months ago 12
Python Question

Wait until process completes using Python WMI

My code launches process on remote host that at the end creates txt file. I want to copy this txt file so I need to wait until the process finish. How can I do that?

import wmi
SW_SHOWNORMAL = 1
con = wmi.WMI(ip, user=username, password=password)
process_startup = con.Win32_ProcessStartup.new()
process_startup.ShowWindow = SW_SHOWNORMAL
process_id, result = con.Win32_Process.Create(CommandLine=command_line, ProcessStartupInformation=process_startup)

Answer

I'm actually working on a similar thing at the moment. If you haven't found your answer, check this link - http://timgolden.me.uk/python/wmi/cookbook.html#run-notepad-wait-until-it-s-closed-and-then-show-its-text

My current Python solution is as follows:

process_id, result = client.Win32_Process.Create(CommandLine=command, CurrentDirectory=directory)
watcher = client.watch_for(
    notification_type="Deletion",
    wmi_class="Win32_Process",
    delay_secs=1,
    ProcessId=process_id
)

watcher()

This seems to work for me.

Comments