1.使用start pythonw xxx.py可在机器上执行python但隐藏终端,通常日志来查问题
2.使用ping实现sleep操作,避免使用pause导致的交互问题
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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
1
2
3
4
5
6
7
8
9
10
|
@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
1
2
3
4
5
6
7
8
|
@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
1
2
3
|
@echo off
if exist app.pid (echo "[%date% %time%] Runningg...")else (echo "[%date% %time%] Stopped..")
ping -n 3 localhost >nul
|
restart.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@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
)
|