在 Windows 上,您可以使用 pythonw.exe 将 python 脚本作为后台进程运行:
Python 脚本(扩展名为 .py 的文件)将由
python.exe 默认情况下。此可执行文件打开一个终端,该终端保持
即使程序使用 GUI 也可以打开。如果您不希望这样
发生,使用扩展名.pyw 这将导致脚本
默认情况下由pythonw.exe 执行(两个可执行文件都位于
Python 安装目录的顶层)。这抑制
启动时的终端窗口。
例如,
C:\ThanosDodd\Python3.6\pythonw.exe C:\\Python\Scripts\moveDLs.py
为了让你的脚本持续运行,你可以使用sched进行事件调度:
sched 模块定义了一个实现通用目的的类
事件调度器
import sched
import time
event_schedule = sched.scheduler(time.time, time.sleep)
def do_something():
print("Hello, World!")
event_schedule.enter(30, 1, do_something, (sc,))
event_schedule.enter(30, 1, do_something, (s,))
event_schedule.run()
现在要杀死 Windows 上的后台进程,您只需运行:
taskkill /pid processId /f
processId 是您要杀死的进程的 ID。