Windows後台執行python程式

2021-06-21 21:32:25 字數 1393 閱讀 1735

import win32serviceutil

import win32service

import win32event

import time

class smallestpythonservice(win32serviceutil.serviceframework):

_svc_name_ = "smallestpythonservice"

_svc_display_name_ = "the smallest possible python service"

def __init__(self, args):

win32serviceutil.serviceframework.__init__(self, args)

# create an event which we will use to wait on.

# the "service stop" request will set this event.

self.hwaitstop = win32event.createevent(none, 0, 0, none)

def svcstop(self):

# before we do anything, tell the scm we are starting the stop process.

self.reportservicestatus(win32service.service_stop_pending)

# and set my event.

win32event.setevent(self.hwaitstop)

def svcdorun(self):

#你需要執行的**#

while true:

f= open('c:/a.log','a')

f.write('asdf\n')

f.close()

time.sleep(5)

win32event.waitforsingleobject(self.hwaitstop, win32event.infinite)

if __name__=='__main__':

win32serviceutil.handlecommandline(smallestpythonservice)

啟動的方法就是直接在cmd下,指令碼名.py install ,然後去windows 的服務下就可以看到the smallest possible python service 這個服務,你可以啟動,停止,還可以設定成開機自動啟動。

有個需要注意的地方:

該指令碼名不能為中文!!否則在啟動服務時會出現「 windows 不能 在本地計算機 啟動 the smallest possible python service .....「的錯誤資訊

windows和linux下打包python程式

一 windows下用py2exe打包python程式生成exe檔案 1 py2exe是一種python發布的打包工具,可以把python指令碼轉換成windows下的可執行程式,不需要安裝python便可執行。3 編寫例程 新建乙個資料夾my py2exe,編寫hello.py程式 hello.p...

sublime text3 如何執行python

前幾天朋友說需要抓取資料分析,在學python。學習過程中問sublime怎麼安裝。於是我也了解了一下。python 是一門開源免費 通用型的指令碼程式語言,它上手簡單,功能強大,堅持 極簡主義 python 是乙個高層次的結合了解釋性 編譯性 互動性和物件導向的指令碼語言。前提是已經安裝好了pyt...

後台執行命令

1.cron是系統主要的排程程序,可以在無需人工干預的情況下執行作業。有乙個叫做crontab的命令允許使用者提交 編輯或刪除相應的作業。2.crob的域 第1列 分鐘1 59 第2列 小時1 23 0表示子夜 第3列 日1 31 第4列 月1 12 第5列 星期0 6 0表示星期天 第6列 要執行...