0

I have written a python function which runs another python script in a remote desktop using PSTools (psexec). I have run the script successfully several times when the function is called only once. But when I call the function multiple times from another file, the subprocess does not run in the second call. In fact it immediately quits the entire program in the second iteration without throwing any exception or Traceback.

    controlpc_clean_command = self.psexecpath + ' -s -i 2 -d -u ' + self.controlPClogin + ' -p ' + self.controlPCpwd + ' \\' + self.controlPCIPaddr + ' cmd.exe /k ' + self.controlPC_clean_code

    logfilePath = self.psexeclogs + 'Ctrl_Clean_Log.txt'
    logfile = file(logfilePath,'w')

    try:

     process = subprocess.Popen(controlpc_clean_command, stdout = subprocess.PIPE,stderr = subprocess.PIPE)


     for line in process.stderr:
      print "** INSIDE SUBPROCESS STDERR TO START PSEXEC **\n"
      sys.stderr.write(line)
      logfile.write(line)


     process.wait()

    except OSError:
      print "********COULD NOT FIND PSEXEC.EXE, PLEASE REINSTALL AND SET THE PATH VARIABLE PROPERLY********\n"

The above code runs once perfectly. Even if I run it from a different python file with different parameters, it runs good. The problem happens when I call the function more than once from one file, then in the second call the function quits after printing "** INSIDE SUBPROCESS STDERR TO START PSEXEC **\n" and it does not even print anything in the main program after that.

I am unable to figure out how to debug this issue. As I am completely clueless where the program goes after printing this line. How do I debug this?

Edit: After doing some search, I added stdout, stderr = subprocess.communicate()

after the subprocess.Popen line in my script. Now, I am able to proceed with the code but with one problem. Nothing is now getting written in the logfile 'Ctrl_Clean_Log.txt' after adding subprocess.communicate() !! How can I write in the file as well as proceed with the code?

0

1 Answer 1

1

Maybe your first process is stuck waiting and blocking other processes.

https://docs.python.org/2/library/subprocess.html

Popen.wait()
    Wait for child process to terminate. Set and return returncode attribute.

Warning This will deadlock when using stdout=PIPE and/or stderr=PIPE and the 
child process generates enough output to a pipe such that it blocks waiting 
for the OS pipe buffer to accept more data. Use communicate() to avoid that.
Sign up to request clarification or add additional context in comments.

1 Comment

I just edited my question with the answer you gave. The problem is, now my process is running good, but the log files are not getting written anymore. Can you please tell how can I write into log file with process.communicate?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.