I am currently using os.system('start cmd.exe /k ' + command)
where “command” is a string that opens an exe file with some arguments.
I know I could avoid calling cmd.exe, but to me it’s easier with that.
When the executable stops, cmd doesn’t close and remains idle waiting for another command; what I want to do is: when the execution finishes, make my python script call the same command again in the same cmd window.
EDIT: I am running multiple instances of cmd.exe which open the same exe with different parameters, my aim is to do the same as above for every instance
returncode = os.system(mycommand)
print(returncode)
prints “0” just after running cmd (without waiting for the execution to end)
I don’t really need the exit code, but parsing the text output from the window might be useful to know whether to stop looping the command or just wait some time before running it again
Related
Whenever I run this program, it completes the first function perfectly and then ends the program before doing anything else. How can I allow the other two functions to run?
import subprocess
subprocess.call(["su", "my_user"]) # runs perfectly
print("user switched to my_user") # does not run
subprocess.call(["cd", "../documents/my_code"]) # does not run
By the way, I am running this from a Linux terminal using ipython.
This isn't doing what you think it is. When you run su, just as the function says, this runs as a subprocess. That subprocess will start as my_user and immediately exit, but your Python process is unaffected. You will still be the original user.
You can feed commands into that subprocess, assuming you need to run things as that other user, but your process isn't going to change.
Followup
subprocess.call waits for the command to finish. The su command is going to create a new shell, logged in as the new user, and that shell will present a new prompt to you. You probably thought your Python script had ended and you were back at the original prompt, but that's not the case. The prompt you're seeing is from su, and you are the new user. If you press Ctrl-D, then the su will exit, your script will continue, and you'll see your script type "user switched to my_user". Thus, you are nested several shells deep and don't realize it. ;)
I'm trying to open the command prompt in a loop from a Python script, and within the command prompt 1.) type to change directory and 2.) type a command. Once the command is entered and running, I'll need to wait for it to finish before moving to the next part of the script, but haven't got that far.
I have the following, which I hope can open cmd and communicate.
import subprocess
process = subprocess.Popen('cmd', stdin=subprocess.PIPE)
process.communicate(b'cd /target/directory')
process.communicate(b'run.bat')
Am I on the right path, and how can I make the script wait until the command has finished then close?
*Edit:
I have a run.bat file within a directory, and would like to run it in the command prompt within its specific directory. I've tried running the .bat file by itself but haven't been successful. I have a cmd shortcut that opens the prompt in the same directory as run.bat, and entering 'run.bat' of course works. As this has been the only thing that worked so far, I'm trying to replicate these steps in a Python script.
To wait for the command to finish then exit you can simply add this instead of the second communicate
process.communicate(b'run.bat && exit 0')
This will run the run.bat file then after it is finished exit with code 0
In Python, How can I check and close such cmd windows like in picture? This code should control and find which cmd is finished and idle like in picture, then close it. I don't want a code which is waiting to close it. It should be a detached program. By the way, the image is a sample. It is not my original problem.
You can think that you have a window or several windows opened and waiting like this, how can they be closed? There is a condition to close a command prompt. This condition is that program on that prompt should be done. Command prompts on which a program runs should not be closed. Moreover, this program should intervene externally and afterwards.
I can't solve with taskkill or psutil. They always see the state of cmd as running. More specifically, you can look at code snippet below,
os.system("start /B start cmd.exe #cmd /k python helloworld.py & exit")
It doesn't close the command prompt.
I don't know if you're looking for this solution, but you can add "& exit" after your script, and that will close CMD.
For example:
python script.py & exit
I have a python script that needs to be run from cmd, but should also stay running once the cmd window has been closed. I've tried various combinations of START and CALL but I've hit a mental blank (and google isn't helping at all).
Here's what I have:
#echo off
start /b python server.py
start python client-sendmessage.py
exit
server.py is a singleton, and should continue running after exit is reached. client-sendmessage.py should halt the script until it returns.
My friend is in a macOS environment and he wanted to call os.system('exit') at the end of his python script to make the terminal close. It doesn't. This doesn't surprise me but I would like to know what exactly is going on between the python script and the terminal when this call is made.
In my mental simulation the terminal should have to tell you that there are still running jobs, but that doesn't happen either.
As a side question : will some less common terminals close when a process calls this?
read the help:
Execute the command (a string) in a subshell.
A subshell is launched, and exit is run in that subshell.
To exit the enclosing terminal, you have to kill the parent. One way to do it is:
os.system("kill -9 %d"%(os.getppid())
The system function starts another shell to execute a command. So in this case your Python scripts starts a shell and runs "exit" command in there, which makes that process exit. However, the Python script itself, including a terminal where it is running, continues to run. If the intent is to kill the terminal, you have to get the parent process ID and send a signal requesting it to stop. That will kill both Python script and a terminal.
Remember that system first spawns/forks a sub-shell to execute its commands. In effect, you are asking only the sub-shell to exit.