I'm trying to call a windows-cmd-file by python's subprocess.Popen module. This script then calls a powershell file that contains the problem logic. The call works fine and the script is executed, anyway it does not seem to terminate.
My current code is the following:
pRun = subprocess.Popen([r'C:\runps.cmd', 'C:\psfile.ps1', 'arg2'], shell=False)
pRun.wait()
... rest of code
So far I am not interested in interaction with the script (like stdin or stdout), I just want to call the script and know when it is terminated.
When I call the same command (C:\runpw.cmd C:\psfile.ps1 arg2) from the windows command line it works fine, doing all the desired actions and then terminates s.t. I can input the next command. That's why I assume that python might wait forever for the termination of a process or thread, that just won't terminate.
Thanks for your help in advance!
post edit:
The code will be executed on a windows machine
Related
My problem
I need to send a command to a popen process that executes a batch file. I have been searching pretty long in the internet for a solution but didn't find anything. The "command" I have to send to is stop.
Tried solutions
I have already tried it with .stdin.write() what ended up in not being able to send commands from the normal console and also the popen process to wait until another execution of the file. Another thing I tried is .communicate() which ended up again with the popen process to wait until another execution of the file.
Current code
Starting code:
mcserver = subprocess.Popen('C:/Users/FlexGames/Desktop/Minecraft_Server/FTBTrident-1.4.0-1.7.10Server/ServerStart.bat',
stdin=subprocess.PIPE)
Current part to send a command to console:
mcserver.stdin.write(b'stop\n')
I have a program that produces a csv file and right at the end I am using os.startfile(fileName) but then due to the program finishing execution the opening file just closes also, same happens if I add a sleep after also, file loads up then once the sleep ends it closes again?
Any help would be appreciated.
From the documentation for os.startfile:
startfile() returns as soon as the associated application is launched. There is no option to wait for the application to close, and no way to retrieve the application’s exit status.
When using this function, there is no way to make your script wait for the program to complete because you have no way of knowing when it is complete. Because the program is being launched as a subprocess of your python script, the program will exit when the python script exits.
Since you don't say in your question exactly what the desired behavior is, I'm going to guess that you want the python script to block until the program finishes execution (as opposed to detaching the subprocess). There are multiple ways to do this.
Use the subprocess module
The subprocess module allows you to make a subprocess call that will not return until the subprocess completes. The exact call you make to launch the subprocess depends heavily on your specific situation, but this is a starting point:
subprocess.Popen(['start', fileName], shell=True)
Use input to allow user to close script
You can have your script block until the user tells the python script that the external program has closed. This probably requires the least modification to your code, but I don't think it's a good solution, as it depends on user input.
os.startfile(fileName)
input('Press enter when external program has completed...')
Quick question on running a batch file using subprocess module in python.
Background
I'm running a .bat file from python and the .bat runs a windows application 100% (takes around a minute) and then waits for input from stdio (x or Esc) to close it.
What I'm doing is:
subprocess.call([r'C:\Users\caSaira\Desktop\myExample.bat'], shell=True)
Since I wasn't sure on how to give x or Esc, I didn't do anything (was under assumption that this will be in waiting stage for input).
But, Looks like the windows application runs in an infinite loop.
Questions
Does anyone know how I can pass 'x' letter, once I complete the execution?
Does anyone know why the application went in infinite loop instead of waiting stage?
I figured it out finally .. I have to use Popen and use the communicate function.
cmd1 = [r'myFile.bat']
p = SP.Popen(cmd1,stdin=SP.PIPE,stdout=SP.PIPE)
output = p.communicate(input='x'.encode())[0]
print(output.decode('ascii'))
thanks for taking the time to read this post. Basically I'm trying to call syntaxnet's parsey mcparseface from a subprocess. For some reason it wont run unless I change the working directory. I can run the subprocess with the following.
process = subprocess.Popen("./syntaxnet/demo.sh", cwd="/home/kahless/models/syntaxnet")
The problem is I also need my script to wait until parsey finishes. So I tried to use the wait command
process.wait()
But for some reason when using .wait() or .communicate() parsey mcparseface wont complete correctly.
I am working currently on Raspbian. My Problem is i have a Python scrip with a infinite Loop that never should stop. In This script i want to call another script without the main script stopping. I tried different methodes to do this like:
import test1
test1.some_func()
or
execfile("test1.py")
or
import subprocess
subprocess.call("python test1.py")
I could start the test1.py script with these solutions, but the script that called it would stop working. So my question is how to start a second script without the first one to stop.
subprocess.call waits for the command to complete and thus blocks your loop. You should use something like process = subprocess.Popen(["python", "test1.py"]). If you want to wait for the process to terminate, you can then call process.wait().