1.使用start pythonw xxx.py可在机器上执行python但隐藏终端,通常日志来查问题
2.使用ping实现sleep操作,避免使用pause导致的交互问题
app.py
import osimport timeimport datetimepid = 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 offif exist app.pid (echo "[%date% %time%] Running.."ping -n 3 localhost >nulexit)else ( echo "[%date% %time%] Starting.."start pythonw app.pyping -n 3 localhost >nulstatus.batping -n 3 localhost >nul)
stop.bat
@echo offif 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 offif exist app.pid (echo "[%date% %time%] Runningg...")else (echo "[%date% %time%] Stopped..")ping -n 3 localhost >nul
restart.bat
@echo offif 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 >nulexit)else ( echo "[%date% %time%] Starting.."start pythonw app.pyping -n 3 localhost >nulstatus.batping -n 3 localhost >nul)