windows使用WinSW注册服务

Windows 使用 WinSW 注册服务

WinSW(Windows Service Wrapper)是一个轻量级工具,可以将任意可执行程序(如 Nginx、Spring Boot 应用等)包装成 Windows 系统服务,实现开机自启动、异常自动重启、后台运行等功能。本文将详细介绍如何使用 WinSW 注册 Windows 服务。

一、下载 WinSW.exe

前往 WinSW 官方 GitHub Releases 页面 下载最新版本的 WinSW-x64.exe(根据系统架构选择对应版本)。

二、修改 WinSW-x64.exe 名字为服务名

将下载好的 WinSW-x64.exe 重命名为你想要的服务名称,例如 nginx-service.exe。注意: 重命名后的 exe 文件名将作为服务注册时的标识,后续所有操作命令都需要使用这个文件名。

三、配置服务 xml

WinSW 通过一个同名的 XML 配置文件来定义服务的各项参数。注意: exe 的名字要和 xml 的一样,比如:WinSW-x64.exe 改成 nginx-service.exe,xml 就要命名为 nginx-service.xml,并放在与 exe 相同的目录下。

案例一:注册 nginx 服务

以下配置将 Nginx 注册为 Windows 服务,支持开机自启动、优雅停止和日志记录:

xml 复制代码
<service>
    <id>nginx</id>
    <name>Nginx</name>
    <description>Nginx Web Server</description>

    <!-- 工作目录 -->
    <workingdirectory>%BASE%</workingdirectory>

    <!-- 启动服务 -->
    <executable>%BASE%\nginx.exe</executable>
    <arguments>-c "%BASE%\conf\nginx.conf"</arguments>

    <!-- 系统自启动 -->
    <startmode>Automatic</startmode>
    <delayedAutoStart>true</delayedAutoStart>

    <!-- 停止服务 -->
    <stopexecutable>%BASE%\nginx.exe</stopexecutable>
    <stoparguments>-s stop</stoparguments>

    <!-- 失败重启(按需启用) -->
    <!-- <onfailure action="restart" delay="10 sec" /> -->

    <!-- WinSW 自身日志(非应用日志) -->
    <logpath>%BASE%\logs</logpath>
    <log mode="roll" />
</service>
案例二:注册 Spring Boot 服务

以下配置将 Spring Boot 应用注册为 Windows 服务,包含 JVM 内存参数、开机自启动和优雅停机设置:

xml 复制代码
<service>
    <id>myTranslation</id>
    <name>myTranslation</name>
    <description>Spring Boot Translation Service</description>

    <workingdirectory>%BASE%</workingdirectory>

    <!-- 修改成服务器实际的 Java 路径 -->
    <executable>D:\tools\jdk\jdk-21_windows-x64_bin\jdk-21.0.10\bin\java.exe</executable>

    <!-- 修改成服务器实际的 Spring Boot JAR 路径 -->
    <arguments>
        -Xms512m
        -Xmx1024m
        -jar "%BASE%\my-Spring-Boot-service-1.0.0.jar"
    </arguments>

    <!-- Windows 开机自动启动 -->
    <startmode>Automatic</startmode>

    <!-- Spring Boot 停止超时时间 -->
    <stoptimeout>180sec</stoptimeout>

    <!-- Spring Boot 异常退出后,10 秒自动重启(按需启用) -->
    <!-- <onfailure action="restart" delay="10 sec" /> -->

    <!-- WinSW 自身日志 -->
    <logpath>%BASE%\logs</logpath>
    <log mode="roll" />
</service>

四、安装服务

配置完成后,在 exe 所在目录打开命令行,依次执行以下命令管理服务:

安装服务
bash 复制代码
nginx-service.exe install
启动服务
bash 复制代码
nginx-service.exe start
停止服务
bash 复制代码
nginx-service.exe stop
重启服务
bash 复制代码
nginx-service.exe restart
卸载服务
bash 复制代码
nginx-service.exe uninstall
刷新服务 xml 配置
bash 复制代码
nginx-service.exe refresh
相关推荐
白搞电子3 小时前
ESP32-S31 项目如何制作 Windows 一键烧录包:完整镜像、esptool 与数据边界
windows·stm32·单片机
apple_692658854 小时前
Github开源免费 红果短剧 Windows 电脑桌面版:民间开发者做的第三方播放器,不用装模拟器了
windows
Vex2une4 小时前
Windows 系统还原发展史:从快照回滚到现代恢复生态
windows·系统还原·系统重置·卷影复制·系统保护·windows re·系统映像备份
Hello_Pyhx4 小时前
挖兔硬盘精灵 WatuDisk:从首次检测到日常检查的使用指南
运维·windows·硬盘·维护
seahahacsdn6 小时前
Windows系统添加VHD双系统
windows·vhd系统·vhd双系统
Hello_Pyhx10 小时前
iPhoneMirror:把 iPhone 接入 Windows,先分清投屏与反控
windows·ios·iphone
kakakahahahaha19 小时前
Win10开始菜单打不开排查:explorer、AppX注册、SFC、服务与用户配置修复
windows·电脑·笔记本电脑·内容运营·软件需求
ITHAOGE1520 小时前
下载 | Windows Server 2016最新纯净版ISO映像!(集成9月更新、标准版、数据中心版、14393.9514)
windows·科技·微软·电脑
传奇开心果编程1 天前
【Compose Multiplatform 跨端开发学与练】第3课 布局与组件
android·windows·学习·ui·ios·kotlin·composer