Quantcast
Channel: ActiveState Community Site - ActivePython discussion
Viewing all articles
Browse latest Browse all 75

How to wait for completion of a remote process using WMI?

$
0
0

I am new to Python and was testing one of the scripts to execute a batch file on a remote machine, which generates an output.txt file there which I want to copy back to my machine once the text file is written. I want to do this only when the batch file has completed its execution and creation of output.txt on remote. How can I do that? This does not happen for me as the WMI starts looking for the output.txt on remote even before the batch file has completed its execution and throws me error. Following is my function:

def run_remote(self,cmd, async=False, minimized=True, output=False):
        """
        this function runs cmd on remote machine, using wmi. the function create a .bat file,
        copies it to the remote machine, and runs the .bat file
        inputs: cmd - command to run
                mimimized - window state
                output - True, returns the command's output
        output: return value of the command
        """

        output_data =None
        pwd=os.getcwd()
        bat_local_path =os.path.join(pwd,'output.bat')
        bat_remote_path =os.path.join(self.remote_path,'output.bat')
        output_remote_path =os.path.join(self.remote_path,'output.txt')
        output_local_path =os.path.join(pwd,'output.txt')
        text =cmd + " > " + output_remote_path
        create_file(bat_local_path, text)
        self.net_copy(bat_local_path,self.remote_path)
        batcmd = bat_remote_path

        SW_SHOWMINIMIZED =0
        ifnot minimized:
            SW_SHOWMINIMIZED =1
        print"Executing '%s' command" %cmd
        startup =self.connection.Win32_ProcessStartup.new(ShowWindow=SW_SHOWMINIMIZED)
        process_id, return_value =self.connection.Win32_Process.Create(CommandLine=batcmd,ProcessStartupInformation=startup)

        """This is where I face the issue"""
        if output andnot async:
            print'copying back ' + output_remote_path
            """This net_copy_back fucntion does not wait for batch file execution to complete.  So it cannot find the output.txt file which is generated after some duration (if I set a debug point here, it then gives enough time for execution of batch file to complete, so the file is then copied successfully)"""
            self.net_copy_back(output_remote_path, output_local_path)
            output_data =open(output_local_path,'r')
            output_data ="".join(output_data.readlines())
            self.net_delete(output_remote_path)
        self.net_delete(bat_remote_path)
        return return_value

AttachmentSize
Working Code.txt6.52 KB

Viewing all articles
Browse latest Browse all 75

Trending Articles