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 )
时间: 2024-10-20 21:40:05