【问题标题】:How to kill a process in window without killing current process如何在不杀死当前进程的情况下杀死窗口中的进程
【发布时间】:2020-06-13 16:11:30
【问题描述】:

你好,我只是不会杀死一个进程而不杀死当前进程假设我有一个 python 代码在 txt 文件中写入一个字符串,例如

import os
while True:
    with open('input.txt' , 'r') as re:
        all_data = re.read()
    if all_data == 'pause':
        os.system('kill.bat')
    else:
        print("\nContinue\n")

在这里它将读取 input.txt,如果它等于 pause 那么它将运行 kill.bat 在这里我想重新启动此代码为此我将编写另一个脚本 kill.bat 重新启动此代码但问题是不是因为它正在杀死 kill.bat 文件而没有重新启动,但我不会只杀死 python 终端并重新启动它我该怎么做?这里是 kill.bat 文件代码

taskkill /IM cmd.exe
python main.py

main.py 是我的 python 文件

【问题讨论】:

    标签: python cmd process


    【解决方案1】:

    很好的尝试@Pryanshu

    你做得对。您当前方法的问题是python,批处理脚本将在同一进程下并同时被杀死。 所以稍微改变一下方法

    这里是要点:

    蟒蛇

    get current process id
    while loop 
        read file
        if matches condition
            call kill.bat and pass this python process ID as separate process
    

    批次

    get first parameter (which is the python process to kill)
    kill the process via taskkill
    start the python script as seprate process
    exit
    

    帮助者

    get current process ID     - pid = os.getpid()
    pass id to batch script    - os.system("kill.bat %s" % pid)
    batch script get arguments - %1
                                 this %1 will contain the first argument passed. 
                                 by default %* will contain all the arguments passed to the script.
    python start program as 
    separate process           - use subprocess python package
    cmd start program as 
    separate process           - use Start command
    kill process via taskkill  - taskkill /F /PID <pid>
                                 replace <pid> with your argument
    

    我知道您可以处理代码部分。您必须利用 Helpers 并将它们组合起来完成任务。让我知道输出。

    如果您有任何问题,请发表评论。我会尽快回复的

    干杯

    【讨论】:

    • 谢谢我试试这个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    相关资源
    最近更新 更多