首页 > 代码库 > windows隐藏python运行时的终端
windows隐藏python运行时的终端
1.使用start pythonw xxx.py可在机器上执行python但隐藏终端,通常日志来查问题
2.使用ping实现sleep操作,避免使用pause导致的交互问题
app.py
import os import time import datetime pid = os.getpid() fp = file("app.pid", "wt") fp.write("%d" % pid) fp.close() while True: time.sleep(1) fp = file("app.log","at") fp.write("%s\n" % datetime.datetime.now()) fp.close()
start.bat
@echo off if exist app.pid (echo "[%date% %time%] Running.." ping -n 3 localhost >nul exit )else ( echo "[%date% %time%] Starting.." start pythonw app.py ping -n 3 localhost >nul status.bat ping -n 3 localhost >nul )
stop.bat
@echo off if exist app.pid (echo "[%date% %time%] Stopping..." python -c "import os; os.system(‘taskkill /F /PID %%s‘ %% open(‘app.pid‘).read());" del app.pid )else (echo "[%date% %time%] Stopped.." ping -n 3 localhost >nul )
status.bat
@echo off if exist app.pid (echo "[%date% %time%] Runningg...")else (echo "[%date% %time%] Stopped..") ping -n 3 localhost >nul
restart.bat
@echo off if exist app.pid (echo "[%date% %time%] Stopping..." python -c "import os; os.system(‘taskkill /F /PID %%s‘ %% open(‘app.pid‘).read());" del app.pid )else (echo "[%date% %time%] Stopped.." ping -n 3 localhost >nul ) if exist app.pid (echo "[%date% %time%] Running.." ping -n 3 localhost >nul exit )else ( echo "[%date% %time%] Starting.." start pythonw app.py ping -n 3 localhost >nul status.bat ping -n 3 localhost >nul )
windows隐藏python运行时的终端
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。