使用时创建Windows服务:
sc create ServiceName binPath= "the path"
Run Code Online (Sandbox Code Playgroud)
如何将参数传递给Installer类的Context.Parameters集合?
我对sc.exe文档的阅读是这样的论证只能在最后传递binPath,但我没有找到一个例子或者能够成功地做到这一点.
我需要运行python脚本并确保它在终止后重新启动.我知道有一个名为supervisord的UNIX解决方案.但不幸的是,我的脚本必须运行的服务器是在Windows上.你知道什么工具有用吗?谢谢
我正在尝试在 PowerShell 中运行以下命令
sc create StrongSwan binpath= "C:\Users\Kanishk\Desktop\Strong\Strong\stronswan\strongswan-5.6.3\src\charon-svc\charon-svc.exe"
Run Code Online (Sandbox Code Playgroud)
我已经检查了 .exe 的路径是否正确,我也可以 cd 到它。作为参考,我正在关注这个:https : //wiki.strongswan.org/projects/strongswan/wiki/Charon-svc
我收到以下错误:
Set-Content : A positional parameter cannot be found that accepts argument 'binpath='.
At line:1 char:1
+ sc create NewService binpath= C:\Users\Kanishk\Desktop\Strong\Strong\ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-Content], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetContentCommand
Run Code Online (Sandbox Code Playgroud)
所以我的问题是相同的命令在 cmd 上运行,但不在 PowerShell 上运行。有什么原因吗?
我正在尝试让一个Flask应用程序在Windows中作为服务运行。我已经尝试按照此处和此处的建议实施解决方案,但没有成功。
我有一个只有两个文件的简单文件夹:
Project
|
+-- myapp.py
+-- win32_service.py
Run Code Online (Sandbox Code Playgroud)
在myapp.py内部是一个简单的Flask应用程序:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
Run Code Online (Sandbox Code Playgroud)
和服务框架win32_service.py:
import win32serviceutil
import win32service
import win32event
import win32evtlogutil
import servicemanager
import socket
import time
import logging
import os
import sys
sys.path.append(os.path.dirname(__name__))
from myapp import app
logging.basicConfig(
filename = r'c:\tmp\flask-service.log',
level = logging.DEBUG,
format = '[flaskapp] %(levelname)-7.7s %(message)s'
)
class HelloFlaskSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "FlaskApp"
_svc_display_name_ …Run Code Online (Sandbox Code Playgroud) 我正在尝试在python中编写一个Windows服务,但棘手的部分是我想将它部署在没有python的机器上.我已经成功地创建了像服务这样的,如果我从我的机器上运行它的工作原理.当我尝试将其转换为exe然后尝试安装它时,问题就开始了.首先我尝试使用cx_freeze服务示例(在这里看到),setup.py看起来像这样:
from cx_Freeze import setup, Executable
options = {'build_exe': {'includes': ['ServiceHandler']}}
executables = [Executable('Config.py', base='Win32Service', targetName='gsr.exe')]
setup(name='GSR',
version='0.1',
description='GSR SERVICE',
executables=executables,
options=options
)
Run Code Online (Sandbox Code Playgroud)
和config.py是:
NAME = 'GSR_%s'
DISPLAY_NAME = 'GSR TEST - %s'
MODULE_NAME = 'ServiceHandler'
CLASS_NAME = 'Handler'
DESCRIPTION = 'Sample service description'
AUTO_START = True
SESSION_CHANGES = False
Run Code Online (Sandbox Code Playgroud)
但是当我尝试构建它(python setup.py build)时,我收到一个错误:"cx_Freeze.freezer.ConfigError:没有名为Win32Service的基地"
其次,我尝试使用常规cx_freeze设置,exe我得到安装服务罚款,但一旦我尝试启动它我得到一个错误:"错误1053:服务没有及时响应启动或控制请求"
setup.py - python 3.3 regualr exe,安装服务,但在尝试启动时发送错误:
from cx_Freeze import setup, Executable
packages = ['win32serviceutil','win32service','win32event','servicemanager','socket','win32timezone','cx_Logging','ServiceHandler']
build_exe_options = {"packages": packages}
executable = [Executable("ServiceHandler.py")]
setup( …Run Code Online (Sandbox Code Playgroud) 我正在使用此处找到的模板:是否可以在Windows中将Python脚本作为服务运行?如果可能,怎么样?
这是我的run.py,我按照上面链接中的说明安装了服务.
from app import app
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
class AppServerSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "Flask App"
_svc_display_name_ = "Flask App"
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.hWaitStop = win32event.CreateEvent(None,0,0,None)
socket.setdefaulttimeout(60)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,''))
self.main()
def main(self):
app.run(host = '192.168.1.6')
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试启动服务时,我收到消息:
"本地计算机上的Flask App服务已启动然后停止.如果某些服务未被其他服务或程序使用,则会自动停止."
知道我做错了什么吗?我尝试了各种用户帐户 - 我不认为这是一个权限问题.
谢谢!
在我昨天发布的另一个问题中,我对如何在Windows中作为服务运行Python脚本提出了很好的建议.我想知道的是:Windows如何了解可以在本机工具中管理的服务("管理工具"中的"服务"窗口).I. e.什么是Windows在Linux下的/etc/init.d中放置启动/停止脚本?
我有一个Windows 7环境,我需要使用Python 3.4开发Python Windows服务.我正在使用pywin32的win32service模块来设置服务,大多数钩子似乎都正常工作.
问题是当我尝试从源代码运行服务时(使用python service.py install后跟python service.py start).这使用PythonService.exe来托管service.py - 但我使用的是venv虚拟环境,脚本无法找到它的模块(发现错误信息python service.py debug).
Pywin32安装在virtualenv中,在查看PythonService.exe的源代码时,它在Python34.dll中动态链接,导入我的service.py并调用它.
运行service.py时如何让PythonService.exe使用我的virtualenv?
1) pip 安装守护进程。
2)打开windows cmd,输入:python,然后输入?导入守护进程终端显示
>>> import daemon
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\daemon\__init__.py", line 42, in <module>
from .daemon import DaemonContext
File "C:\Python27\lib\site-packages\daemon\daemon.py", line 25, in <module>
import pwd
ImportError: No module named pwd
>>>
Run Code Online (Sandbox Code Playgroud)
3)pip安装密码
有什么问题?????
我有一个小的Web服务器应用程序,我用Python编写,从数据库系统获取一些数据并将其作为XML返回给用户.这部分工作正常 - 我可以从命令行运行Python Web服务器应用程序,我可以让客户端连接到它并获取数据.目前,要运行Web服务器,我必须以管理员用户身份登录到我们的服务器,并且必须手动启动Web服务器.我希望Web服务器在系统启动时自动启动为服务并在后台运行.
使用ActiveState的站点和StackOverflow中的代码,我非常清楚如何创建服务,我想我已经排序了 - 我可以安装并启动我的Web服务器作为Windows服务.但是,我无法弄清楚如何再次停止服务.我的Web服务器是从BaseHTTPServer创建的:
server = BaseHTTPServer.HTTPServer(('', 8081), SIMSAPIServerHandler)
server.serve_forever()
Run Code Online (Sandbox Code Playgroud)
自然地,serve_forever()调用使Web服务器处于无限循环中并等待HTTP连接(或ctrl-break按键,对服务无用).我从上面的示例代码中得到了一个想法,即你的main()函数应该处于一个无限循环中,只有当它处于"停止"状态时才会突破它.我的主要调用serve_forever().我有一个SvcStop功能:
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
exit(0)
Run Code Online (Sandbox Code Playgroud)
当我从命令行执行"python myservice stop"时,我似乎被调用了(我可以在其中放置一个产生输出到文件的调试行)但实际上并没有退出整个服务 - 后续调用"python myservice start" "给我一个错误:
启动服务时出错:服务实例已在运行.
随后的停止呼叫给了我:
停止服务时出错:此时服务无法接受控制消息.(1061)
我想我需要替换serve_forever(serve_until_stop_received,或其他),或者我需要一些修改SvcStop的方法,以便它停止整个服务.
这是一个完整的列表(我已经修剪了包含/注释以节省空间):
class SIMSAPIServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
try:
reportTuple = self.path.partition("/")
if len(reportTuple) < 3:
return
if reportTuple[2] == "":
return
os.system("C:\\Programs\\SIMSAPI\\runCommandReporter.bat " + reportTuple[2])
f = open("C:\\Programs\\SIMSAPI\\out.xml", "rb")
self.send_response(200)
self.send_header('Content-type', "application/xml")
self.end_headers()
self.wfile.write(f.read())
f.close()
# The output from CommandReporter is simply dumped to out.xml, which we read, …Run Code Online (Sandbox Code Playgroud) python ×8
windows ×7
service ×3
flask ×2
pywin32 ×2
cx-freeze ×1
daemon ×1
pwd ×1
pyinstaller ×1
python-3.4 ×1
python-3.x ×1
supervisord ×1
virtualenv ×1
webserver ×1