windows隐藏python运行时的终端
生活随笔
收集整理的這篇文章主要介紹了
windows隐藏python运行时的终端
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
From:?https://my.oschina.net/sanpeterguo/blog/337263
摘要: windows上如果想執行一些常駐進程,使用python需要打開終端,容易誤操作關掉。本文介紹一種在后臺啟動的解決方法,其實核心功能很簡單,使用 start pythonw xxx.py 進行,附加一些常用的運維批處理腳本 start.bat stop.bat restart.bat status.bat1.使用start pythonw xxx.py可在機器上執行python但隱藏終端,通常日志來查問題
2.使用ping實現sleep操作,避免使用pause導致的交互問題
app.py
import?os import?time import?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
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?off if?exist?app.pid?(echo?"[%date%?%time%]?Runningg...")else?(echo?"[%date%?%time%]?Stopped..") ping?-n?3?localhost?>nulrestart.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 )總結
以上是生活随笔為你收集整理的windows隐藏python运行时的终端的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 8.2设备文件及磁盘分区
- 下一篇: Python3中urllib详细使用方法