Skip to content

Usage as a windows service #45

@bmappi

Description

@bmappi

Hello,

On Windows when creating a service:

@echo off
setlocal
set SERVICE_NAME=ModbusGateway
set DISPLAY_NAME=Modbus Server Service
set INSTALL_DIR=C:\ModbusGateway
set EXECUTABLE=%INSTALL_DIR%\omodsim.exe
set CONFIG_DIR=%INSTALL_DIR%\conf\TestConfig
set EXEC_CMD="%EXECUTABLE%" --config "%CONFIG_DIR%"

sc create "%SERVICE_NAME%" binPath= "%EXEC_CMD%" DisplayName= "%DISPLAY_NAME%" start= auto
sc start "%SERVICE_NAME%"

The executable does not reply in a manner that windows SCM expect for a service. Quick solution is to use things like NSSM but not ideal.

To work with Windows service, the main should detects if it's launched by the SCM (Service Control Manager), and runs in service mode.

I currently made an experimental version that supports it (using the --service flag): https://github.com/bmappi/OpenModSim/blob/main/omodsim/main.cpp#L152

C:\ModbusGateway\omodsim.exe --service --config C:\ModbusGateway\conf\TestConfig

My code is very bad, I am not c++ developer and just tried to make it work. The handler is limited and only supports running the server as subprocess (stopping / restarting won't work, also losing its child process won't start it again)

Also, it looks like there is a bug on windows (running on Windows 10 22H2) with command line arguments not displayed on the console. My fix was to use QMessageBox to show the text, but better would be hooking the parent console if present when using command line arguments.

Good software, reliable. Thank you for your good work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions