杀死后台程序
# -*- coding: utf-8 -*- """ Created on Fri Apr 10 21:16:57 2020 @author: Administrator """ #执行代码测试 #控制程序自动执行 import os import datetime import time import psutil def check(): key=False pids = psutil.pids() for pid in pids: p = psutil.Process(pid) if p.name() == '爬取DJ中文歌曲.exe': print("程序还在:") return True else: pass return key def run(): kill() os.chdir(r"e://dist/") path = "爬取DJ中文歌曲.exe" print("运行爬取DJ中文歌曲.exe进程") os.system(path) def kill(): pids = psutil.pids() for pid in pids: p = psutil.Process(pid) if p.name() == '爬取DJ中文歌曲.exe': print("杀死爬取DJ中文歌曲.exe进程") cmd = 'taskkill /F /IM 爬取DJ中文歌曲.exe' os.system(cmd) def main(h1=5,h2=12): run() while True: # 判断后台是否还有程序 if check(): pass else: run() if __name__ == '__main__': main()
-----------------------------------------------------------------------------------------------------------------------------------------