I want to force shut down the .exe file that was booted from the .bat file.

Asked 1 years ago, Updated 1 years ago, 303 views

There is a run.bat file and a test.exe file and run.bat via Python as follows:

run.bat

SETLOCAL
SET PATH = %PATH%;..\..\samples\external\opencv\bin;..\..\bin;
test.exe

python_test.py

importos
os.chdir(os.path.dirname(os.path.abspath(__file__)))

from os import path
import subprocess

runPath=path.join(path.dirname(__file__), 'run.bat')
process=subprocess.Popen(runPath)

import time
time.sleep(10)

import signal
import psutil
try:
    os.kill (process.pid, signal.SIGTERM)

except:
    import traceback
    print(traceback.format_exc())

try:
    parent=psutil.Process(process.pid)
    for child in parent.children (recursive=True):
        child.kill()
    parent.kill()
except:
    import traceback
    print(traceback.format_exc())

If you open the exe file directly with popen, you can get the process.pid of the exe file with the return value, so you can terminate it, but you couldn't do it through the .bat file.

The error is as follows:

Traceback (most recent call last):
  File "C:\Users\python_test.py", line 23, in <module>
    parent=psutil.Process(process.pid)
  File "C:\Users\Documents\WPy64-31040\python-3.10.4.amd64\lib\site-packages\psutil\_init__.py", line 332, in_init__
    self._init(pid)
  File "C:\Users\Documents\WPy64-31040\python-3.10.4.amd64\lib\site-packages\psutil\_init__.py", line 373, in_init
    raise NoSuchProcess(pid,msg='process PID not found')
psutil.NoSuchProcess: process PID not found (pid=10976)

I also tried to run the .bat file for forced termination, but I couldn't.

taskkill.bat

taskkill/F/T/IM test.exe

Python runs in WinPython.
Is there a way to force the termination?

python

2022-11-28 21:02

1 Answers

It seems that this has been resolved in the comments, so I will write down the content as an answer.

On the source code provided, you have already terminated it with os.kill(process.pid, signal.SIGTERM), so it is an exception because the process does not exist.

The psutil processing of kill() and os.kill() processing order were reversed to finish.

However, there are some questions.

Since psil seems to kill the original process with parent.kill(), changing the order of processing means os.kill() for the pid of the same original process, and no exceptions occurred there? That's what it means.
Well, if there are no exceptions, then there is no problem.

There is another article for reference.
Kill subprocess to child process and stop (python)

Windows Simply call the kill() method and the external commands executed by the shell will also stop.

cmd="some shell command"# Shell Run Commands
p=subprocess.Popen(cmd)
p.kill()

Therefore, the source code of the question contains the return value of subprocess.Popen() in process, so you may be able to finish everything with process.kill().
Try it.


2022-11-29 09:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.