Run as a service
A gateway that you start from a terminal stops when the terminal closes. To start the gateway at boot and restart it after a failure, register it with the service manager of the operating system. This page describes systemd on Linux and WinSW on Windows.
Before you start
- Follow Install from npm.
- Run the gateway in the foreground and check that it works. A configuration problem is difficult to find through a service manager.
Why the stop path matters
The gateway releases its licence seat while it shuts down.
It handles SIGINT, SIGTERM, SIGHUP and SIGBREAK, runs its cleanup hooks and exits.
The shutdown has a time limit of 8 seconds. Set SHUTDOWN_GRACE_MS to change it.
The signal that arrives depends on the platform:
| Signal | On Linux | On Windows |
|---|---|---|
SIGINT | Ctrl+C | Ctrl+C: delivered |
SIGBREAK | Not used. | Ctrl+Break: delivered |
SIGTERM | systemctl stop, docker stop | Never delivered. Windows has no mechanism for it. |
SIGHUP | Terminal hangup. | Only when the console window closes, never when a service stops. |
On Linux, SIGTERM is the stop path. On Windows, the two console events are the stop path.
The gateway handles both.
A service definition must give the gateway enough time to stop. A supervisor that kills the process 1 second after the signal cuts the licence release short. The settings below allow 45 seconds.
A hard kill does not lose the seat. The seat stays bound to the stable identity of the machine, and the next start uses it again. A clean stop returns a floating seat to the pool at once.
How to run as a service on Linux with systemd
-
Create a dedicated user and set the installation folder:
sudo useradd --system --create-home --home-dir /opt/omni-edge omniedgesudo chown -R omniedge:omniedge /opt/omni-edge -
Write the file
/etc/systemd/system/opcua-omni-edge.service:[Unit]Description=OPC UA Omni-Edge gatewayDocumentation=https://opcua-omni-edge.doc.sterfive.com/After=network-online.targetWants=network-online.target[Service]Type=simpleUser=omniedgeGroup=omniedgeWorkingDirectory=/opt/omni-edgeExecStart=/opt/omni-edge/node_modules/.bin/opcua-omni-edge run -c /opt/omni-edge/config.yaml# The gateway stores its licence activation and stable machine identity# under $HOME/.sterfive/opcua-omni-edge. systemd does not set HOME for# system services, so pin it: without this the identity can land# somewhere unexpected and a restart consumes a new licence seat.Environment=HOME=/opt/omni-edgeRestart=on-failureRestartSec=5# Let the licence release finish. KillSignal is SIGTERM by default, which# the gateway handles; TimeoutStopSec must comfortably exceed the 8s# internal shutdown budget.KillSignal=SIGTERMTimeoutStopSec=45[Install]WantedBy=multi-user.target -
Load the unit and start the service:
sudo systemctl daemon-reloadsudo systemctl enable --now opcua-omni-edge -
Look at the state and the log:
systemctl status opcua-omni-edgejournalctl -u opcua-omni-edge -fIf the configuration file has an error, the gateway stops with the exit code 1 before it uses a licence seat. The journal shows
CONFIG-E001.Restart=on-failurestarts the gateway again after 5 seconds. Correct the configuration file.
How to check that it works
- Run
sudo systemctl stop opcua-omni-edge. - Look at the journal. After a clean stop, it shows
licence lease released. - If the line is not there, the stop was cut short. Check
TimeoutStopSec.
How to harden the service
Add these lines to [Service]:
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=/opt/omni-edge
ReadWritePaths must include the installation folder, the certificate store and $HOME/.sterfive.
The gateway writes to all three.
How to run as a service on Windows with WinSW
WinSW makes a Windows service from an executable and one XML file.
-
Download
WinSW.exeinto the installation folder. -
Rename it to match the XML file, for example
omni-edge-service.exeandomni-edge-service.xml. -
Write the XML file:
<service><id>opcua-omni-edge</id><name>OPC UA Omni-Edge</name><description>Sterfive OPC UA Omni-Edge gateway</description><executable>node</executable><arguments>node_modules\@sterfive\opcua-omni-edge\opcua-omni-edge.js run -c config.yaml</arguments><workingdirectory>C:\omni-edge</workingdirectory><onfailure action="restart" delay="10 sec"/><stoptimeout>45 sec</stoptimeout><log mode="roll-by-size"><sizeThreshold>10240</sizeThreshold><keepFiles>3</keepFiles></log></service> -
In an elevated prompt, install and start the service:
.\omni-edge-service.exe install.\omni-edge-service.exe start.\omni-edge-service.exe status
Do not use the .cmd shim as <executable>.
The shim starts an intermediate cmd.exe, and the service manager stops cmd.exe and not the gateway.
Call node on the entry point of the package, as in the sample.
How to check that it works
WinSW stops a console application with a console control event.
The gateway receives Ctrl+Break as SIGBREAK and runs its cleanup hooks.
A wrapper that ends the process instead skips the licence release without a message.
Check the stop one time after the installation.
-
Run:
.\omni-edge-service.exe stop -
Look at the log. A clean stop writes these two lines:
shutting down (SIGBREAK) ...shutdown complete. -
SIGINTinstead ofSIGBREAKis also correct. It means that the wrapper usedCtrl+C. -
If the lines are missing and the exit code is
-1073741510(0xC000013A,STATUS_CONTROL_C_EXIT), the process stopped before a hook ran. Increase<stoptimeout>and test again.
The account of the service
The licence activation and the stable identity of the machine are in %USERPROFILE%\.sterfive\opcua-omni-edge.
%USERPROFILE% is the profile of the account that runs the service.
A service that runs as LocalSystem uses C:\Windows\system32\config\systemprofile.
It does not see an activation that you made with your own account, and the gateway starts without a licence.
Use one of these solutions:
- Run the service as the account that made the activation.
- Set
OPCUA_OMNI_EDGE_ACTIVATION_KEY_FILEto a file that the service account can read. This does not depend on the account. Use it for unattended installations.
About pm2
pm2 is a Node.js process manager. pm2 startup makes the boot registration.
pm2 is correct for a development or pilot machine: one command works on every operating system.
Do not use pm2 in production, for two reasons:
- The default
kill_timeoutof pm2 is 1600 ms. The gateway needs up to 8 seconds. pm2 sends SIGKILL during the release at each restart. If you use pm2, setkill_timeout: 30000in the ecosystem file. - On Windows, pm2 cannot send signals. It ends the process, and the cleanup hooks do not run. No setting changes this. The seat stays bound to the machine and the next start uses it again, but the gateway never releases it early.
systemd exists on every Linux gateway, needs no extra program and gives exact control of the stop path.