最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - after window close program.exe file still running in background - Stack Overflow

programmeradmin0浏览0评论

I have a script that runs a program.exe in the background. How to close a program from the background in task manager after closing the command window script

script

command = [
        "program.exe",
        "-i",
        "-o",
    ]


    process = subprocess.Popen(
        command,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
        bufsize=1,
        universal_newlines=True,
    )
    stdout, stderr = processmunicate()

print(command)

I have a script that runs a program.exe in the background. How to close a program from the background in task manager after closing the command window script

script

command = [
        "program.exe",
        "-i",
        "-o",
    ]


    process = subprocess.Popen(
        command,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
        bufsize=1,
        universal_newlines=True,
    )
    stdout, stderr = process.communicate()

print(command)
Share Improve this question asked Feb 5 at 18:54 Oussama GamerOussama Gamer 513 bronze badges 4
  • Yes, "program.exe" will continue to run - this is the nature of subprocess.Popen: you've created a separate process and told it to run a program. You'll have to set up something else in your script to tell it to kill that program when the script exits. – JRiggles Commented Feb 5 at 19:08
  • I found another code to kill the process using the "psutil". The problem is that when merging the two codes, it kills the program process before it completes processing. Do you have a better idea? – Oussama Gamer Commented Feb 5 at 19:14
  • Let me see if I understand correctly: you use Python to launch a separate program, that program must exit when Python exits, but not before it has completed some task? Should Python wait to exit until the subprocess is complete? Otherwise your options are: 1. Python exits and the subprocess keeps going, 2. Python exits and kills the subprocess (regardless of whether or not its work is done), or 3. Python attempts to exit and must wait until the subprocess is done with its work (and either exits on its own or communicates with Python that it can be closed). – JRiggles Commented Feb 5 at 19:22
  • Script that processes multiple files in a window command duplicate. Even though I used another script to kill the process. The script is running at the same time before it completes processing. import os os.system("script.py && prockill.py) – Oussama Gamer Commented Feb 5 at 19:40
Add a comment  | 

1 Answer 1

Reset to default 0

Subprocess popen creates an separate process that will consiste after your script exectuded an ended. So to end that proces you should explicitly command it to end.

process.terminate()

will gracefully shut it down while

process.kill()

can force kill it.

You can use process.poll() to check if it still runs so you can better do

process.terminate()
time.sleep(1)
if process.poll() is None:
    process.kill()

This tells program to cleanly close but if it doesnt close you can kill after waiting

发布评论

评论列表(0)

  1. 暂无评论