I have python script that interacts with other windows program. Occasionally this program crashes and I get an error in script. How can I shut down this program and restart it again?
This code works for me
import subprocess
subprocess.call(["taskkill", "/F", "/T", "/IM", "progname.exe"]) # forced killing, with childs, by name
subprocess.Popen([r"C:\Path\To\Folder\progname.exe", "--fast"])
Related
I need to close an .exe at the end of my code. I was able to start the .exe file
proc = subprocess.Popen('.nameProgram.exe')
Now I have to close it but the terminate() function doesn't seem to work.
I tried this code:
proc.terminate()
I noticed that the exe executable is under another java process. How can I close it. Do you have any suggestions? Thanks
Type
ps
In terminal & check on which name program is runing once you run. copy that name.
#inside python program
import os
os.system('pkill programName')
I am trying to launch an android emulator from Python. I have tried the following code:
os.system('C:\\Nox\\bin\\Nox.exe -clone:Nox')
subprocess.Popopen('C:\\Nox\\bin\\Nox.exe -clone:Nox')
The emulator launched by either code closes as soon as python code is terminated. However, when I run the code ('C:\\Nox\\bin\\Nox.exe -clone:Nox') in Win10 terminal, the emulator doesn't close when the terminal is closed.
How can I keep the emulator running when python code terminates? I do not want to keep python code running.
I don't have a Windows machine to try this on, but in Ubuntu the following did it for me:
import subprocess
subprocess.Popen('<your command string>', shell=True)
So in your case:
import subprocess
subprocess.Popen('C:\\Nox\\bin\\Nox.exe -clone:Nox', shell=True)
Note there is a parameter creationFlags with values that seem of interest (https://docs.python.org/3/library/subprocess.html#windows-constants), however hopefully shell=True will suffice.
Do note the strong warnings in the documentation around opening a process with shell=True where the process being run depends upon some user input!
I want to remote control 'my_program' using os.
import os
os.system(my_program)
While in debug mode in VS-Code i can start the 'my_program', but as soon as it opens, i've no available thread in VS-Code to work with. Or atleast that's what i'm interpreting by the message 'Unable to find thread for evaluation.'. I can not execute any commands in the debug console anymore, for instance 3+3, which should output 6.
As soon as i manually kill 'my_program' by simply closing it in the GUI, i can continue debugging.
In Short:
What i want: Open, use and Close 'my_program' with VSCode using os.system
What i get: Program opens, but VS debugging is somewhat offline, since it is 'Unable to find thread for evaluation.' and i can not continue debugging/closing the program via os.system
os.system("TASKKILL /F /IM my_program.exe")
EDIT: i can reproduce the same behaviour (freezed debugging console) using
subprocess.call(my_program)
I had the same error message. The introspection just works when the debugger is stopped at a breakpoint. I've put a breakpoint in the code, stopped in it and everything works fine.
It works with
subprocess.Popen(my_program)
subprocess.call and os.system block the terminal as long as the process being called is finished, thus 'freezing' the thread and debug window. subprocess.popen on the other hand is an asynchronous call which let's you interact with the terminal while the called process keeps running in the background
I have a python script named update.py, and I want to use another python script to check whether the script is running or not. If update.py doesn't run or error, then the script will run update.py.
Can i do it? If there is an example it will be very thankful.
Not sure about what you are asking but as given this may help, so if you just want to call one python script from another then you can use script 1
#!/usr/bin/python
from subprocess import call
call(["python", "update.py"])
Save this file in a script named script1 and run it, it will compile update.py.
If you want to check for any syntax error in update.py then you can use script 2
#!/usr/bin/python
from subprocess import call
call(["python","-m","py_compile", "update.py"])
If script2 compiles without any error then it shows that there is no syntax error in your program.
Thirdly if you want to check if update.py is running currently or not you can use script 3
#!/usr/bin/python
import psutil
import sys
from subprocess import Popen
for process in psutil.process_iter():
if process.cmdline() == ['python', 'update.py']:
sys.exit('Process found: exiting.')
print('Process not found: starting it.')
Popen(['python', 'update.py'])
This last script will tell if your script is running or not and if it is not running it will compile it.
Scripts are generally used for these kinds of tasks. You have a monitor script that keeps track of update.py that keeps running in the background. It becomes easier if monitor script launches the python script in the beginning.
#!/bin/bash
# Monitor script.
EXEC=<path>/update.py
while true; do
"$EXEC" &
wait # Here the assumption is that you want to run this forever.
done
Is it possible to somehow quit Python IDLE under Windows from within my python code/script at the end after running?
I tried something like:
import sys
sys.exit(0) # or exit(1)
didn't work. Probably only quits the current "process" of the running python script.
Thanks a lot
If you can run IDLE from the command line (or edit your shortcut), I found this in the IDLE help.
If IDLE is started with the -n command line switch it will run in a
single process and will not create the subprocess which runs the RPC
Python execution server.
Which seems to imply that the script is running inside IDLE. I did a quick test script, and calling exit() brought up a box asking to kill the program. Clicked yes, and the script and IDLE both quit. Hope that can help.
This will do exactly what you want. No prompt.
import os
os.system("taskkill /f /im pythonw.exe")
To quit Python IDLE under Windows from within a python code/script without a further prompt, try:
parent_pid = os.getppid() # Get parent's process id
parent_process = None
for proc in psutil.process_iter(attrs=['pid']): # Check all processes
if proc.pid == parent_pid: # Parent's Process class
parent_process = proc
break
if parent_process is not None:
parent_process.kill() # Kill the parent's process
This works with IDLE, PyCharm and even the command line in Windows.