如何停止Windows启动python脚本

时间:2018-10-05 17:57:53

标签: python windows startup

我有一个名为forever.py的python脚本,该脚本通过Windows本地组策略编辑器运行。脚本位于目录C:\Windows\System32\GroupPolicy\Machine\Scripts\Startup中。这是该脚本

import subprocess
from time import sleep
import logging.config

# Makes server_receiver.py run forever unless there is an error with this script

LOGGER_NAME = 'STARTUP_RECEIVER'
logging.config.fileConfig('C:\\Automation\\startup\\config\\startup_receiver.conf')
logger = logging.getLogger(LOGGER_NAME)


while True:
    p = subprocess.Popen(['python', r'C:\Automation\startup\hello.py'],
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output, error = p.communicate()
    rc = p.returncode
    if rc != 0:
        logger.debug('{}'.format(error))
        sleep(5)
        continue
    else:
        logger.debug('Test Passed with return code {}'.format(rc))
        sleep(5)
        pass

该脚本仅运行另一个名为hello.py的python脚本,该脚本显示hello。由于此脚本在启动时开始运行并在后台运行,因此我想知道是否可以通过任何方式将任何内容添加到该脚本中或添加到另一个脚本中,以使我永远杀死。我的想法是,由于该脚本在后台运行,因此我无法使用argparse添加名为“ -stop”的选项,该选项将杀死当前的pid。我想到的唯一其他解决方案是用另一个脚本杀死该脚本,但我不确定该怎么做。我不相信还有另一种方法来获取另一个正在运行的脚本的pid,就像Linux中那样。有谁知道我在这里可以做什么?另外,由于启动脚本目录加载在C:\ windows \ system32中,因此可能存在一些权限问题。

0 个答案:

没有答案