如何停止windows启动python scrip

2024-04-18 11:19:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python脚本叫做永远.py我正在通过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。由于这个脚本在启动时开始并在后台运行,所以我想知道是否有任何方法可以将任何内容添加到脚本或其他脚本中,从而使我能够杀死它永远.py. 我的想法是,因为这个脚本是在后台运行的,所以我不能使用argparse添加一个名为'stop'的选项来终止当前的pid。我唯一想到的另一个解决办法是用另一个脚本杀死这个脚本,我不确定我该怎么做。我不相信还有其他方法可以像linux那样获取另一个正在运行的脚本的pid。有人知道我能在这里做什么吗?此外,可能存在一些权限问题,因为启动脚本目录加载在C:\windows\system32中。你知道吗


Tags: pyimport目录脚本confighellowindowslogging