1. Use the start pythonw xxx.py to execute Python on the machine but hide the terminal, usually log to check for problems
2. Use ping to implement sleep operations and avoid interaction problems caused by the use of 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 ' percent 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 ' percent 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)
Windows hides the Python runtime terminal