Skip to content

Commit

Permalink
'update'
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiyu liu committed Aug 16, 2024
1 parent 5066eb3 commit e895163
Showing 1 changed file with 1 addition and 84 deletions.
85 changes: 1 addition & 84 deletions _posts/2024-08-16-NSSM.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ tags: [NSSM]
---





**此篇文章是由于我搞不定 Python 的 Windows 服务而诞生的。**

NSSM(Non-Sucking Service Manager)是一款优秀的Windows服务管理工具,它为开发者和系统管理员提供了一种简单而强大的方式来将普通应用程序转换为Windows服务。以下是对NSSM的详细介绍:
Expand Down Expand Up @@ -48,87 +45,7 @@ NSSM(Non-Sucking Service Manager)是一款优秀的Windows服务管理工具

### 使用bat脚本来生成和启动服务

```bat
@echo off
:: Check for administrator privileges
net session >nul 2>&1
if %errorlevel% neq 0 (
echo Administrator privileges required, requesting...
powershell -command "Start-Process '%~f0' -Verb RunAs"
exit /b
)
echo Administrator privileges obtained.
REM Set paths and service name
SET NSSM_PATH=%~dp0nssm.exe
SET SERVICE_NAME=AExcelToCsvService
SET SERVICE_PATH=%~dp0excel_to_csv.exe
SET STARTUP_DIR=%~dp0
SET LOG_PATH=%STARTUP_DIR%service.log
REM Remove trailing backslash from STARTUP_DIR if present
if %STARTUP_DIR:~-1%==\ SET STARTUP_DIR=%STARTUP_DIR:~0,-1%
REM Debug output
echo Current directory: %CD%
echo Script directory: %~dp0
echo NSSM path: %NSSM_PATH%
echo Service path: %SERVICE_PATH%
echo Startup directory: %STARTUP_DIR%
echo Log path: %LOG_PATH%
echo Checking if executable exists...
if not exist "%SERVICE_PATH%" (
echo ERROR: Executable not found at %SERVICE_PATH%
pause
exit /b
)
echo Checking directory permissions...
dir "%STARTUP_DIR%" >nul 2>&1
if %errorlevel% neq 0 (
echo ERROR: Cannot access directory %STARTUP_DIR%
pause
exit /b
)
REM Check if the service exists
sc query %SERVICE_NAME% >nul 2>&1
if %errorlevel% equ 0 (
echo Service %SERVICE_NAME% exists. Stopping the service...
%NSSM_PATH% stop %SERVICE_NAME%
echo Updating service settings...
%NSSM_PATH% set %SERVICE_NAME% Application "%SERVICE_PATH%"
%NSSM_PATH% set %SERVICE_NAME% AppDirectory "%STARTUP_DIR%"
%NSSM_PATH% set %SERVICE_NAME% AppStdout "%LOG_PATH%"
%NSSM_PATH% set %SERVICE_NAME% AppStderr "%LOG_PATH%"
%NSSM_PATH% set %SERVICE_NAME% Start SERVICE_DELAYED_AUTO_START
) else (
echo Service %SERVICE_NAME% does not exist. Installing...
%NSSM_PATH% install %SERVICE_NAME% "%SERVICE_PATH%"
%NSSM_PATH% set %SERVICE_NAME% AppDirectory "%STARTUP_DIR%"
%NSSM_PATH% set %SERVICE_NAME% AppStdout "%LOG_PATH%"
%NSSM_PATH% set %SERVICE_NAME% AppStderr "%LOG_PATH%"
%NSSM_PATH% set %SERVICE_NAME% Start SERVICE_DELAYED_AUTO_START
)
REM Verify service settings
echo Verifying service settings...
%NSSM_PATH% get %SERVICE_NAME% Application
%NSSM_PATH% get %SERVICE_NAME% AppDirectory
%NSSM_PATH% get %SERVICE_NAME% AppStdout
%NSSM_PATH% get %SERVICE_NAME% AppStderr
REM Start the service
echo Starting service %SERVICE_NAME%...
%NSSM_PATH% start %SERVICE_NAME%
echo Service %SERVICE_NAME% has been updated and started
echo Please check %LOG_PATH% for any startup errors
pause
```




Expand Down

0 comments on commit e895163

Please sign in to comment.