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')
Related
I want to run a program from inside my Python script and get the PID of the process I launched. I tried with :
p=subprocess.Popen(nameofmyprocess) pid=p.pid
But the problem is that it doesn't wait for my called program to finish. When I looked at the documentation, I concluded I should use subprocess.run() instead, however it doesn't have a .pid like Popen
Is there another alternative ?
Edit :
I should have mentioned this in the original question. The code of my program includes a server part where it opens a socket and listens to it. I have a client side which connects to that socket and sends a message. My end goal is to write a script that runs my program, gets its PID to pass it to some functions I wrote that will do the monitoring of my program : they will show me the memory usage of my program, the socket it has opened, the file descriptors.. basically information about things my program does.
As suggested , I used subprocess.Popen.poll so now my code looks like this :
p=subprocess.Popen(nameofmyprocess)
pid=p.pid
print pid
while (p.poll() is None):
time.sleep(20)
myFunc(pid)
myFunc2(pid)
However, when I run this script and run my client, it can't connect to my server program. It says "Connection failed". I'm pretty sure the program is running though because the PID I print is displayed when I use the command ps aux on another terminal.
Summary
I am automating xfoil with the subprocess module. I would like to be able to start an xfoil session with several commands and leave it open for the user to take on.
This would help debugging and also more generally to have a basic routine to start xfoil (without manually typing the same set of commands every time).
I am able to run any xfoil command using subprocess.communicate().
However, when open with subprocess xfoil systematically closes without user action.
Example
With the following code, you can see xfoil opening and closing quickly.
import subprocess
XFOIL_PATH = 'xfoil.exe'
xfoil = subprocess.Popen(XFOIL_PATH, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
actions = 'NACA 0012\nGDES\n'
xfoil.communicate(input=actions)
Note
I've used subprocess.Popen() with Rhino and Rhino stays open until I close it manually. I do not understand why the behavior is different with xfoil.
I suspect it has something to do with the specific application's stdout but it's a wild guess. Hopefully it is possible to do something about it.
My understanding is when you call communicate() with the input parameter, the call will close stdin, which terminates the xfoil.exe process. Try the following instead of calling communicate():
xfoil.stdin.write(actions)
xfoil.stdin.flush()
After that, the process continues until you exit your script.
Update
If you want the xfoil project to continue even after your script ends, please look into pexpect.
is there a way to restart another script in another shell?
i have script that sometimes stuck waiting to read email from gmail and imap. from another script i would like to restart the main one but without stopping the execution of the second
i have tried:
os.system("C:\Users\light\Documents\Python\BOTBOL\Gmail\V1\send.py")
process = subprocess.Popen(["python", "C:\Users\light\Documents\Python\BOTBOL\Gmail\V1\send.py"])
but both run the main in the second's shell
EDIT:
sorry, for shell i mean terminal window
After your last comment and as the syntax show that you are using Windows, I assume that you want to launch a Python script in another console. The magic word here is START if you want that the launching execute in parallel with the new one, or START /W if you want to wait for the end of the subprocess.
In your case, you could use:
subprocess.call(["cmd.exe", "/c", "START", "C:\Path\To\PYTHON.EXE",
"C:\Users\light\Documents\Python\BOTBOL\Gmail\V1\send.py"])
Subprocess has an option called shell which is what you want. Os calls are blocking which means that only after the command is completed will the interpreter move to the next line. On the other hand subprocess popens are non blocking, however both these commands will spawn off child process from the process running this code. If you want to run in shell and get access shell features to execute this , try the shell = True in subprocess.
I could try and explain everything you need but I think this video will do it better: Youtube Video about multithreading
This will allow you to run 2 things f.e.
Have 1 run on checkin email and the other one on inputs so it wont stop at those moments and making multiple 'shelves' possible, as they are parallel.
If you really want to have a different window for this, i am sorry and I can not help.
Hope this was were you were looking for.
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'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