Python-在Windows上创建没有管理员权限的服务
我有以下代码:Python-在Windows上创建没有管理员权限的服务,python,windows,python-3.x,service,background,Python,Windows,Python 3.x,Service,Background,我有以下代码: from os.path import splitext, abspath from sys import modules import win32serviceutil import win32service import win32event import win32api def main(): # do something class Service(win32serviceutil.ServiceFramework): _svc_name_ =
from os.path import splitext, abspath
from sys import modules
import win32serviceutil
import win32service
import win32event
import win32api
def main():
# do something
class Service(win32serviceutil.ServiceFramework):
_svc_name_ = '_unNamed'
_svc_display_name_ = '_Service Template'
def __init__(self, *args):
win32serviceutil.ServiceFramework.__init__(self, *args)
self.log('init')
self.stop_event = win32event.CreateEvent(None, 0, 0, None)
def log(self, msg):
import servicemanager
servicemanager.LogInfoMsg(str(msg))
def sleep(self, sec):
win32api.Sleep(sec*1000, True)
def SvcDoRun(self):
self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
try:
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
self.log('start')
self.start()
self.log('wait')
win32event.WaitForSingleObject(self.stop_event, win32event.INFINITE)
self.log('done')
except Exception as x:
self.log('Exception : %s' % x)
self.SvcStop()
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
self.log('stopping')
self.stop()
self.log('stopped')
win32event.SetEvent(self.stop_event)
self.ReportServiceStatus(win32service.SERVICE_STOPPED)
# to be overridden
def start(self): pass
# to be overridden
def stop(self): pass
def instart(cls, name, display_name=None, stay_alive=True):
''' Install and Start (auto) a Service
cls : the class (derived from Service) that implement the Service
name : Service name
display_name : the name displayed in the service manager
stay_alive : Service will stop on logout if False
'''
cls._svc_name_ = name
cls._svc_display_name_ = display_name or name
try:
module_path=modules[cls.__module__].__file__
except AttributeError:
# maybe py2exe went by
from sys import executable
module_path=executable
module_file = splitext(abspath(module_path))[0]
cls._svc_reg_class_ = '%s.%s' % (module_file, cls.__name__)
if stay_alive: win32api.SetConsoleCtrlHandler(lambda x: True, True)
try:
win32serviceutil.InstallService(
cls._svc_reg_class_,
cls._svc_name_,
cls._svc_display_name_,
startType = win32service.SERVICE_AUTO_START
)
print ('Install ok')
win32serviceutil.StartService(
cls._svc_name_
)
print ('Start ok')
except Exception as x:
print (str(x))
startmain() # to start the main function of the program
Service = Service
instart = instart
class Test(Service):
def start(self):
self.runflag=True
while self.runflag:
self.sleep(10)
self.log("I'm alive ...")
def stop(self):
self.runflag=False
self.log("I'm done")
instart(Test, 'aTest', 'Python Service Test')
已成功创建并启动该服务,但具有管理员权限。如果我以普通用户的身份执行脚本,它将失败。有没有一种方法可以在没有管理员权限的情况下执行此操作?我还注意到,这个类不仅将脚本作为服务启动,还将在Services.msc上创建一个脚本。但我不需要它。我可以把我的脚本自动运行(这是可行的没有管理员perm。)。。重要的是我的脚本已经在后台开始了。我可以这样做吗?如果您只是想避免创建控制台窗口,请使用pythonw.exe运行脚本。如果要在会话0中使用单个实例,请使用启动时触发的任务,该任务在用户是否登录的情况下运行。@eryksun我需要一种方法使脚本从脚本本身在后台运行,而不是与外部应用程序(如pythonw.exe)一起运行。我没有得到其他部分或评论,你能更具体一点吗?Python脚本是通过Python.exe、pythonw.exe或冻结的可执行文件运行的。如果脚本是通过pythonw.exe运行的,那么这是一个什么样的外部应用程序?@eryksun感谢您不断的回答。对于外部应用程序,我的意思是打开我的脚本,默认情况下它将使用python.exe而不是pythonw.exe,我需要所有操作都是自动化的。第三方用户不必从python.py ti pythonw.py修改程序。我需要在双击myscript.py之后,它已经在后台启动了。pyw应该与pythonw.exe关联。至于一般用法,如果它是作为便携式应用程序分发的,那么您应该使用PyInstaller或py2exe将其包装为冻结的可执行文件。这些工具使您可以选择使用控制台或GUI存根可执行文件。