Inno Setup脚本注册Windows服务后无法启动Node SSR服务求助
问题:Inno Setup注册Node服务后无法启动,显示“暂停”状态
在使用Inno Setup脚本安装程序时,需注册Windows服务以启动Node服务器运行Angular SSR构建,但服务创建后显示“暂停”状态,手动启动/暂停时报错。
可正常运行的Node启动命令
"C:\Program Files\nodejs\node" "C:\Program Files (x86)\envVarIssue\ecom\angular\server\main.js"
该命令在CMD中运行完全正常,但通过Windows服务启动失败。安装过程中同时部署了Node.msi,服务创建后无法启动,状态始终为暂停。
报错现象
- 启动服务时提示“启动Windows服务时出错”
- 服务状态显示为“暂停”
使用的工具
通过nssm工具注册Windows服务,以下为完整Inno Setup脚本代码:
; Script generated by the Inno Setup Script Wizard. ; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES! #define MyAppName "envVarIssue" #define MyAppVersion "1.5" #define MyAppPublisher "My Company, Inc." #define MyAppURL "https://www.example.com/" #define MyAppExeName "MyProg.exe" #define MyAppAssocName MyAppName + " File" #define MyAppAssocExt ".myp" #define MyAppAssocKey StringChange(MyAppAssocName, " ", "") + MyAppAssocExt #define MyAppParentPath "." #define MyAppShortName "ecomNodeServer" #define NODE64 "node-v21.6.2-x64.msi" #define NSSM64 "nssm.exe" [Setup] SetupLogging=yes ChangesEnvironment=yes ; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications. ; (To generate a new GUID, click Tools | Generate GUID inside the IDE.) AppId={{D2C055CB-72E7-4DB9-83EE-34D399939D0C} AppName={#MyAppName} AppVersion={#MyAppVersion} ;AppVerName={#MyAppName} {#MyAppVersion} AppPublisher={#MyAppPublisher} AppPublisherURL={#MyAppURL} AppSupportURL={#MyAppURL} AppUpdatesURL={#MyAppURL} DefaultDirName={autopf}\{#MyAppName} ChangesAssociations=yes DisableProgramGroupPage=yes ; Uncomment the following line to run in non administrative install mode (install for current user only.) PrivilegesRequired=admin OutputBaseFilename=envVarIssue App Compression=lzma SolidCompression=yes WizardStyle=modern [Languages] Name: "english"; MessagesFile: "compiler:Default.isl" [Tasks] Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked [Files] Source: "C:\Program Files (x86)\Inno Setup 6\Examples\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion; Source: "{#MyAppParentPath}\ecom\angular\*"; DestDir: "{app}\ecom\angular"; Flags: ignoreversion recursesubdirs createallsubdirs; ;Source: "{#MyAppParentPath}\ecom\node-v20.11.1-win-x64\*"; DestDir: "{app}\ecom\node-v20.11.1-win-x64"; Flags: ignoreversion recursesubdirs createallsubdirs; ; uncomment for you want to install node via .msi Source: "{#MyAppParentPath}\winstaller\{#NODE64}"; DestDir: "{app}"; Flags: ignoreversion; Source: "{#MyAppParentPath}\ecom\nssm-2.24\win64\{#NSSM64}"; DestDir: "{app}"; Flags: ignoreversion ; NOTE: Don't use "Flags: ignoreversion" on any shared system files [Icons] Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}" Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon [Run] ; Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent ; install service without installation node ;Filename: "{app}\{#NSSM64}"; \ Parameters: "install {#MyAppShortName} ""{app}\ecom\node-v20.11.1-win-x64\node.exe"" ""{app}\ecom\angular\server\main.js"""; \ Description: "Installing Node Server service"; \ StatusMsg: "Installing Node Server service..."; \ Flags: runascurrentuser runhidden; ;Filename: "{cmd}"; \ Parameters: "/c nssm set {#MyAppShortName} description ""test"" && nssm set {#MyAppShortName} DisplayName ""ECOM NODE SERVER"" && nssm set {#MyAppShortName} start SERVICE_DELAYED_AUTO_START"; \ WorkingDir: "{app}"; \ Description: "Configure Node Server service"; \ StatusMsg: "Configure Node Server service..."; \ Flags: runascurrentuser runhidden; ;Filename: "{app}\{#NSSM64}"; \ Parameters: "start {#MyAppShortName}"; \ Description: "Configure Node Server service"; \ StatusMsg: "Configure Node Server service..."; \ Flags: runascurrentuser runhidden; Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: postinstall skipifsilent; ; Install Node Filename: "{sys}\msiexec.exe"; Parameters: "/passive /i ""{app}\{#NODE64}"""; ; Add Firewall Rules Filename: "{sys}\netsh.exe"; Parameters: "advfirewall firewall add rule name=""Node In"" program=""{pf64}\nodejs\node.exe"" dir=in action=allow enable=yes"; Flags: runhidden; Filename: "{sys}\netsh.exe"; Parameters: "advfirewall firewall add rule name=""Node Out"" program=""{pf64}\nodejs\node.exe"" dir=out action=allow enable=yes"; Flags: runhidden; ; Add System Service Filename: "{app}\{#NSSM64}"; Parameters: "install {#MyAppShortName} ""{pf64}\nodejs\node.exe"" ""{app}\ecom\angular\server\main.js"""; Flags: runhidden; Filename: "{sys}\net.exe"; Parameters: "start {#MyAppShortName}"; Flags: runhidden; ;Filename: "{cmd}"; Parameters: "/C set PATH=%PATH%;{app}\mongodb\bin"; Flags: runascurrentuser waituntilterminated; ; Run MongoDB mongod command ;Filename: "{cmd}"; Parameters: "/K mongod >> ""{app}\mongo_logfile.txt"" 2>&1"; Flags: postinstall waituntilterminated runascurrentuser; [UninstallRun] Filename: "{app}\{#NSSM64}"; Parameters: "remove {#MyAppShortName} confirm"; Flags: runhidden; Filename: "{app}\{#NSSM64}"; Parameters: "stop {#MyAppShortName}"; Flags: runhidden; ; Removes System Service ;Filename: "{sys}\net.exe"; Parameters: "stop {#MyAppShortName}"; Flags: runhidden; ;Filename: "{app}\{#NSSM64}"; Parameters: "remove {#MyAppShortName} confirm"; Flags: runhidden; ;Filename: "{sys}\net.exe"; Parameters: "stop {#MyAppLCShortName}"; Flags: runhidden; ;Filename: "{pf64}\nodejs\node.exe"; Parameters: "{app}\bin\windows-service-installer.js remove"; Flags: runhidden; [Code] procedure DebugOutput(Msg: string); begin Log('Debug: ' + Msg); end; [Code]
排查及解决方法
确保Node安装完成后再操作服务
当前脚本中安装Node.msi后立即启动服务,但msi安装可能未完全完成(如环境变量配置、组件注册)。需在Node安装命令添加waituntilterminated标志:Filename: "{sys}\msiexec.exe"; Parameters: "/passive /i ""{app}\{#NODE64}"""; Flags: waituntilterminated;配置服务工作目录
Node运行Angular SSR时需要在项目根目录执行,否则可能找不到依赖。添加nssm配置命令:Filename: "{app}\{#NSSM64}"; Parameters: "set {#MyAppShortName} AppDirectory ""{app}\ecom\angular"""; Flags: runhidden;检查服务运行权限
默认Local System账户可能缺少文件访问权限,可配置服务使用管理员账户运行:Filename: "{app}\{#NSSM64}"; Parameters: "set {#MyAppShortName} ObjectName ""本地管理员账户"" ""账户密码"""; Flags: runhidden;启用日志定位错误
通过nssm输出服务日志,查看具体启动失败原因:Filename: "{app}\{#NSSM64}"; Parameters: "set {#MyAppShortName} AppStdout ""{app}\service_out.log"""; Flags: runhidden; Filename: "{app}\{#NSSM64}"; Parameters: "set {#MyAppShortName} AppStderr ""{app}\service_err.log"""; Flags: runhidden;修正卸载顺序
卸载时需先停止服务再移除,调整[UninstallRun]段顺序:Filename: "{app}\{#NSSM64}"; Parameters: "stop {#MyAppShortName}"; Flags: runhidden; Filename: "{app}\{#NSSM64}"; Parameters: "remove {#MyAppShortName} confirm"; Flags: runhidden;
内容的提问来源于stack exchange,提问作者Harsh Patel
相关产品推荐
相关产品推荐

