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 )
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。