代码之家  ›  专栏  ›  技术社区  ›  German Latorre

从可执行文件创建Windows服务

  •  282
  • German Latorre  · 技术社区  · 14 年前

    6 回复  |  直到 11 年前
        1
  •  509
  •   CodeCaster    9 年前

    要从可执行文件创建Windows服务,可以使用 sc.exe :

    sc.exe create <new_service_name> binPath= "<path_to_the_service_executable>"
    

    exe 路径,以及 binPath= .

    More information on the sc command can be found in Microsoft KB251192 .

    请注意,它不会只适用于任何可执行文件:可执行文件必须是Windows服务( i.e. implement ServiceMain

    有一些工具可以从任意的非服务可执行文件创建Windows服务,请参阅其他答案以获取此类工具的示例。

        2
  •  272
  •   Brian Webster Jason    6 年前

    使用NSSM( 非吸吮服务经理 )将.BAT或任何.EXE文件作为服务运行。

    http://nssm.cc/

    • 第一步 :下载NSSM
    • 第2步 :使用安装服务 nssm.exe install [serviceName]
    • :这将打开一个用于定位可执行文件的GUI
        3
  •  99
  •   user285594 user285594    7 年前

    第一步: 下载和解压缩 nssm-2.24.zip

    第二步:

    C:\> nssm.exe install [servicename]

    它将按如下方式打开GUI(示例为UT2003 server),然后只需浏览到:yourapplication.exe

    enter image description here

    更多信息: https://nssm.cc/usage

        4
  •  20
  •   Tobias Kienzler    6 年前

    许多现有的答案包括安装时的人工干预。这可能是一个容易出错的过程。如果您有许多可执行文件想作为服务安装,那么您最不想做的就是在安装时手动执行它们。

    serman ,一个命令行工具,用于将可执行文件作为服务安装。只需编写(而且只需编写一次)一个简单的服务配置文件以及可执行文件。跑

    serman install <path_to_config_file>
    

    将安装服务。 stdout stderr 都被记录了。有关更多信息,请查看 project website .

    工作配置文件非常简单,如下所示。但它也有许多有用的特性,例如 <env> <persistent_env> 在下面。

    <service>
      <id>hello</id>
      <name>hello</name>
      <description>This service runs the hello application</description>
    
      <executable>node.exe</executable>
    
      <!-- 
           {{dir}} will be expanded to the containing directory of your 
           config file, which is normally where your executable locates 
       -->
      <arguments>"{{dir}}\hello.js"</arguments>
    
      <logmode>rotate</logmode>
    
      <!-- OPTIONAL FEATURE:
           NODE_ENV=production will be an environment variable 
           available to your application, but not visible outside 
           of your application
       -->
      <env name="NODE_ENV" value="production"/>
    
      <!-- OPTIONAL FEATURE:
           FOO_SERVICE_PORT=8989 will be persisted as an environment
           variable to the system.
       -->
      <persistent_env name="FOO_SERVICE_PORT" value="8989" />
    </service>
    
        5
  •  11
  •   Timo Rash    4 年前

    这些额外的证明是有用的。。需要作为一个 管理员

    sc create  <service_name> binpath=<binary_path>
    sc stop    <service_name>
    sc queryex <service_name>
    sc delete  <service_name>
    

        6
  •  4
  •   A. Masson    7 年前

    我已经测试了一个很好的产品: AlwaysUp . 不是免费的,但他们有30天的试用期,所以你可以试一试。。。

        7
  •  3
  •   stackprotector    4 年前

    等同于 Sergii Pozharov's answer ,但使用PowerShell cmdlet:

    New-Service -Name "MyService" -BinaryPathName "C:\Path\to\myservice.exe"
    

    New-Service 更多定制。

    这只适用于已经实现 Windows Services API

        8
  •  1
  •   Sergey Vaulin    4 年前

    您可以查看我的小型免费实用程序,以执行服务创建\编辑\删除操作。以下是创建示例:

    转到服务->修改->创建

    enter image description here

    [Download]

    源代码: [Download]

    博客帖子: [BlogLink]

    WinServiceUtils.cs

        9
  •  0
  •   Tomeg    4 年前

    也许你所有的答案都更好,但是——为了完成选项的选择——我想提醒一下多年来使用的古老的类似方法:

    斯瓦尼

    如下所述: https://docs.microsoft.com/en-us/troubleshoot/windows-client/deployment/create-user-defined-service

        10
  •  0
  •   CubicleSoft    4 年前

    几年前,我创建了跨平台Service Manager软件,以便在Windows、Mac和Linux操作系统上启动PHP和其他脚本语言作为系统服务:

    https://github.com/cubiclesoft/service-manager

    服务管理器是一组预编译的二进制文件,它们使用几乎相同的命令行选项在目标操作系统上安装和管理系统服务( source code also available

    如果子进程死亡,服务管理器将自动重新启动它。

    使用服务管理器启动的进程应该定期监视两个通知文件来处理重新启动和重新加载请求,但它们不一定要这样做。如果服务管理器没有及时响应受控的重新启动/重新加载请求,它将强制重新启动子进程。