潇洒郎: python windows Bat文件后台运行

一个偶然的发现,os.system运行命令行命令-执行bat文件,线程设置守护线程后可以让bat文件后台运行,即使程序结束了,bat中运行的exe仍然在运行。

潇洒郎: python windows Bat文件后台运行_第1张图片

 潇洒郎: python windows Bat文件后台运行_第2张图片

 

或者命令行查询进程

tasklist | find "redis-server.exe"

潇洒郎: python windows Bat文件后台运行_第3张图片

 后台运行代码如下

def t1():
  os.system(f'"{batfiel_path}" \n\n\n')
t = threading.Thread(target=t1)
t.setDaemon(True)  # 后台执行,程序结束仍然运行
t.start()

全部代码如下

import threading, os, time
data = r'''[func]
# kill=1 杀死后台进程, kill=0后台运行
kill=0
# 要杀死的后台进程名称
pro

你可能感兴趣的:(python,windows,bat后台运行)