wiki:IntegratingOpenvpnWithNssm

Version 1 (modified by Samuli Seppänen, 9 years ago) (diff)

--

Integrating OpenVPN with NSSM

NSSM ("Non-sucking Service Manager") is a public domain service manager for Windows. It tries to ensure that processes launched by it are respawned should they die. In addition it can write processes' output into logfiles and manage log rotation, among other things. For details have a look at the NSSM website and the README.txt file bundled in the nssm zip-file.

Installing NSSM

NSSM is distributed as a zipfile which contains binaries for 32- and 64-bit Windows. To install nssm.exe just extract the zip-file and copy the appropriate executable somewhere in the path, e.g. C:\Program Files\OpenVPN\bin.

Adding an OpenVPN connection to NSSM

Making NSSM monitor an OpenVPN connection is quite straightforward:

set BASEDIR=C:\Program Files\OpenVPN
set NSSM=%BASEDIR%\bin\nssm.exe
set CONN=community.ovpn

"%NSSM%" status %CONN% > NUL 2>&1
if %ERRORLEVEL% EQU 3 (
    "%NSSM%" install %CONN% "%BASEDIR%\bin\openvpn.exe" > NUL 2>&1
)

"%NSSM%" set %CONN% AppDirectory "%BASEDIR%\config" > NUL 2>&1
"%NSSM%" set %CONN% AppParameters "--config %CONN%" > NUL 2>&1
"%NSSM%" set %CONN% AppStdin "%BASEDIR%\log\%CONN%.log" > NUL 2>&1
"%NSSM%" set %CONN% AppStdout "%BASEDIR%\log\%CONN%.log" > NUL 2>&1
"%NSSM%" set %CONN% AppStderr "%BASEDIR%\log\%CONN%.log" > NUL 2>&1
"%NSSM%" set %CONN% AppRotateFiles 1 > NUL 2>&1
"%NSSM%" set %CONN% DependOnService Dhcp tap0901 > NUL 2>&1
"%NSSM%" start %CONN% > NUL 2>&1

The CONN variable refers to the OpenVPN connection file you wish to launch.

Acknowledgements

Thanks to Jason Haar for providing the initial version of the above script!