Files
XCDesktop/remote/docs/指南/NSSM使用指南.md
2026-03-08 01:34:54 +08:00

8.6 KiB
Raw Permalink Blame History

NSSM 使用指南

NSSM (Non-Sucking Service Manager) 是一个将任意应用程序安装为 Windows 服务的工具。


一、NSSM 简介

NSSM 可以将 Node.js 应用、Python 脚本、批处理文件等任何可执行程序安装为 Windows 服务,实现开机自启动和自动重启。

本地文件位置

nssm/
└── nssm.exe    # 64 位版本

二、安装服务

基本语法

nssm install <服务名> <程序路径> [参数]

安装 Remote 应用服务

# 以管理员身份运行 PowerShell
cd C:\Users\xuanchi\Desktop\remote\nssm

# 安装服务
.\nssm.exe install RemoteApp "C:\Program Files\nodejs\node.exe" "C:\Users\xuanchi\Desktop\remote\index.js"

使用图形界面安装

# 打开图形界面
.\nssm.exe install RemoteApp

在弹出的窗口中配置:

标签页 配置项
Application Path C:\Program Files\nodejs\node.exe
Application Startup directory C:\Users\xuanchi\Desktop\remote
Application Arguments index.js
I/O Output (stdout) C:\Users\xuanchi\Desktop\remote\logs\service.log
I/O Error (stderr) C:\Users\xuanchi\Desktop\remote\logs\error.log

三、管理服务

启动服务

# 通过 NSSM
.\nssm.exe start RemoteApp

# 或通过 Windows 命令
net start RemoteApp

# 或通过 sc 命令
sc start RemoteApp

停止服务

# 通过 NSSM
.\nssm.exe stop RemoteApp

# 或通过 Windows 命令
net stop RemoteApp

# 或通过 sc 命令
sc stop RemoteApp

重启服务

.\nssm.exe restart RemoteApp

查看服务状态

.\nssm.exe status RemoteApp

编辑服务配置

# 打开图形界面编辑
.\nssm.exe edit RemoteApp

删除服务

# 先停止服务
.\nssm.exe stop RemoteApp

# 删除服务
.\nssm.exe remove RemoteApp

# 确认删除(不弹窗)
.\nssm.exe remove RemoteApp confirm

四、完整安装示例

一键安装脚本

创建 install-service.ps1

# 以管理员身份运行
$nssmPath = "C:\Users\xuanchi\Desktop\remote\nssm\nssm.exe"
$nodePath = "C:\Program Files\nodejs\node.exe"
$appPath = "C:\Users\xuanchi\Desktop\remote"
$logPath = "C:\Users\xuanchi\Desktop\remote\logs"

# 创建日志目录
if (-not (Test-Path $logPath)) {
    New-Item -ItemType Directory -Path $logPath
}

# 安装服务
& $nssmPath install RemoteApp $nodePath "index.js"

# 设置工作目录
& $nssmPath set RemoteApp AppDirectory $appPath

# 设置显示名称
& $nssmPath set RemoteApp DisplayName "Remote Desktop Application"

# 设置描述
& $nssmPath set RemoteApp Description "Remote screen streaming service"

# 设置启动类型(自动)
& $nssmPath set RemoteApp Start SERVICE_AUTO_START

# 设置日志输出
& $nssmPath set RemoteApp AppStdout "$logPath\service.log"
& $nssmPath set RemoteApp AppStderr "$logPath\error.log"

# 设置日志轮转
& $nssmPath set RemoteApp AppRotateFiles 1
& $nssmPath set RemoteApp AppRotateBytes 1048576

# 设置失败重启
& $nssmPath set RemoteApp AppExit Default Restart
& $nssmPath set RemoteApp AppRestartDelay 5000

# 启动服务
& $nssmPath start RemoteApp

Write-Host "RemoteApp service installed and started successfully!"

运行安装脚本

# 以管理员身份运行
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
.\install-service.ps1

五、高级配置

设置环境变量

.\nssm.exe set RemoteApp AppEnvironmentExtra "NODE_ENV=production" "PORT=3000"

设置服务依赖

# 依赖其他服务(如需要网络)
.\nssm.exe set RemoteApp DependOnService Tcpip

设置用户账户

# 使用特定用户运行
.\nssm.exe set RemoteApp ObjectName ".\username" "password"

设置优先级

# 设置进程优先级
.\nssm.exe set RemoteApp AppPriority NORMAL_PRIORITY_CLASS

# 可选值:
# IDLE_PRIORITY_CLASS
# BELOW_NORMAL_PRIORITY_CLASS
# NORMAL_PRIORITY_CLASS
# ABOVE_NORMAL_PRIORITY_CLASS
# HIGH_PRIORITY_CLASS
# REALTIME_PRIORITY_CLASS

设置失败操作

# 第一次失败:重启服务
.\nssm.exe set RemoteApp AppExit Default Restart

# 重启延迟(毫秒)
.\nssm.exe set RemoteApp AppRestartDelay 5000

# 设置 Windows 服务恢复选项
.\nssm.exe set RemoteApp AppThrottle 1500

六、同时安装 FRP 和 Remote 服务

完整安装脚本

创建 install-all-services.ps1

# 以管理员身份运行
$nssmPath = "C:\Users\xuanchi\Desktop\remote\nssm\nssm.exe"
$nodePath = "C:\Program Files\nodejs\node.exe"
$appPath = "C:\Users\xuanchi\Desktop\remote"
$frpPath = "C:\Users\xuanchi\Desktop\remote\frp"
$logPath = "C:\Users\xuanchi\Desktop\remote\logs"

# 创建日志目录
if (-not (Test-Path $logPath)) {
    New-Item -ItemType Directory -Path $logPath
}

# ==================== 安装 FRP 客户端服务 ====================

Write-Host "Installing FRP Client service..."

& $nssmPath install FRPClient "$frpPath\frpc.exe" "-c frpc.toml"
& $nssmPath set FRPClient AppDirectory $frpPath
& $nssmPath set FRPClient DisplayName "FRP Client"
& $nssmPath set FRPClient Description "FRP Client for Remote Access"
& $nssmPath set FRPClient Start SERVICE_AUTO_START
& $nssmPath set FRPClient AppStdout "$logPath\frpc-service.log"
& $nssmPath set FRPClient AppStderr "$logPath\frpc-error.log"
& $nssmPath set FRPClient AppRotateFiles 1
& $nssmPath set FRPClient AppRotateBytes 1048576
& $nssmPath set FRPClient AppExit Default Restart
& $nssmPath set FRPClient AppRestartDelay 5000

# ==================== 安装 Remote 应用服务 ====================

Write-Host "Installing Remote App service..."

& $nssmPath install RemoteApp $nodePath "index.js"
& $nssmPath set RemoteApp AppDirectory $appPath
& $nssmPath set RemoteApp DisplayName "Remote Desktop Application"
& $nssmPath set RemoteApp Description "Remote screen streaming service"
& $nssmPath set RemoteApp Start SERVICE_AUTO_START
& $nssmPath set RemoteApp AppStdout "$logPath\service.log"
& $nssmPath set RemoteApp AppStderr "$logPath\error.log"
& $nssmPath set RemoteApp AppRotateFiles 1
& $nssmPath set RemoteApp AppRotateBytes 1048576
& $nssmPath set RemoteApp AppExit Default Restart
& $nssmPath set RemoteApp AppRestartDelay 5000

# 设置 RemoteApp 依赖 FRPClient
& $nssmPath set RemoteApp DependOnService FRPClient

# ==================== 启动服务 ====================

Write-Host "Starting services..."

& $nssmPath start FRPClient
Start-Sleep -Seconds 3
& $nssmPath start RemoteApp

Write-Host "All services installed and started successfully!"
Write-Host ""
Write-Host "Services installed:"
Write-Host "  - FRPClient (FRP 客户端)"
Write-Host "  - RemoteApp (远程桌面应用)"
Write-Host ""
Write-Host "Access URL: http://146.56.248.142:8080"

七、卸载服务

卸载脚本

创建 uninstall-services.ps1

# 以管理员身份运行
$nssmPath = "C:\Users\xuanchi\Desktop\remote\nssm\nssm.exe"

# 停止并删除 RemoteApp
Write-Host "Removing RemoteApp service..."
& $nssmPath stop RemoteApp
& $nssmPath remove RemoteApp confirm

# 停止并删除 FRPClient
Write-Host "Removing FRPClient service..."
& $nssmPath stop FRPClient
& $nssmPath remove FRPClient confirm

Write-Host "All services removed successfully!"

八、常用命令速查

操作 命令
安装服务 nssm install <服务名> <程序> [参数]
启动服务 nssm start <服务名>
停止服务 nssm stop <服务名>
重启服务 nssm restart <服务名>
查看状态 nssm status <服务名>
编辑配置 nssm edit <服务名>
删除服务 nssm remove <服务名>
设置参数 nssm set <服务名> <参数名> <值>
获取参数 nssm get <服务名> <参数名>

九、常见问题

1. 服务启动后立即停止

原因: 程序路径错误或依赖未满足

解决:

# 检查服务日志
type C:\Users\xuanchi\Desktop\remote\logs\error.log

# 检查程序路径
nssm get RemoteApp Application

2. 权限不足

解决: 以管理员身份运行 PowerShell

3. 端口被占用

# 查看端口占用
netstat -ano | findstr :3000

# 结束进程
taskkill /PID <PID> /F

4. 服务无法访问网络

解决:

# 设置服务依赖网络
nssm set RemoteApp DependOnService Tcpip Dhcp Dnscache

十、参考链接