How to best run wine program as systemd service? Type=forking? PID of start.exe or wineserver?

Questions about Wine on Linux
Locked
cj000
Level 2
Level 2
Posts: 12
Joined: Mon Apr 03, 2023 2:06 pm

How to best run wine program as systemd service? Type=forking? PID of start.exe or wineserver?

Post by cj000 »

I have a piece of text mode Windows program that listens on TCP port 25. In Windows the program would normally run as a Windows Service without console output.

My Arch Linux kernel 6.2.9 with wine-8.5 has no X installed, it's text mode.

The program is tested to run from a user login. Now the program should run automatically, as the same user, without user login. My idea is to run the program on Linux as daemon via a systemd service. So service management can be done from the Linux command prompt, like other running daemons.

Running as a specific user is possible with systemd: [Service] section must in include a "User=" line, or use systemd --user session.

Systemd service type
Choosing a systemd service type is more challenging. I think service type should be Type=forking. Though that requires a PID file to be generated. A 2001 wineserver pid file patch didn't make it into wine. No PID file is generated when running wine or wineconsole.

Type=forking PIDFile=?
The lack of PID file generation might be worked around with a pidof command.
Is it then better to use the PID of start.exe or the PID of /usr/bin/wineserver?

PS Tips and tricks how to run a Windows executable as Linux daemon are welcome.
jkfloris
Level 12
Level 12
Posts: 3201
Joined: Thu Aug 14, 2014 10:10 am

Re: How to best run wine program as systemd service? Type=forking? PID of start.exe or wineserver?

Post by jkfloris »

PS Tips and tricks how to run a Windows executable as Linux daemon are welcome.
An example for the Diablo Game Server can be found here:
viewtopic.php?p=113281#p113281

This example is much simpler, but does have a handy ExecStop in it:
https://github.com/Fuseteam/systemd-ser ... al.service
cj000
Level 2
Level 2
Posts: 12
Joined: Mon Apr 03, 2023 2:06 pm

Re: How to best run wine program as systemd service? Type=forking? PID of start.exe or wineserver?

Post by cj000 »

Another complex Wine systemd service file example is at https://github.com/7thCore/isrsrv-scrip ... 40.service

Most complex up till now is how to get the WINEDLLOVERRIDES Environment var set correctly in the systemd .service unit file.
No good is:

Code: Select all

[Service]
Environment="WINEDLLOVERRIDES=explorer.exe,services.exe=d"
ExecStart=/usr/sbin/wineconsole "/home/wineuser/mbserver.exe"
Also tried without improvement:

Code: Select all

[Service]
ExecStart=/usr/bin/bash -c 'env WINEDLLOVERRIDES="explorer.exe,services.exe=d" /usr/sbin/wineconsole "/home/wineuser/mbserver.exe"'
Last edited by cj000 on Fri Apr 14, 2023 11:22 am, edited 2 times in total.
cj000
Level 2
Level 2
Posts: 12
Joined: Mon Apr 03, 2023 2:06 pm

Re: How to best run wine program as systemd service? Type=forking? PID of start.exe or wineserver?

Post by cj000 »

Thanks invisible_kid.

It's all my bad (manually comparing systemctl status output with the wrong output lines), actually my first try:

Code: Select all

[Service]
Environment="WINEDLLOVERRIDES=explorer.exe,services.exe=d"
ExecStart=/usr/sbin/wineconsole "/home/wineuser/mbserver.exe"
runs ok, and outputs this as status:

Code: Select all

systemd[1]: Started Xwall spam filter.
wineconsole[822]: 002c:err:wineboot:start_services_process Couldn't start services.exe: error 126
wineconsole[824]: 0034:err:win:get_desktop_window failed to start explorer c0000135
wineconsole[834]: 0034:err:win:get_desktop_window failed to start explorer c0000135
wineconsole[834]: 005c:err:winediag:nodrv_CreateWindow Application tried to create a window, but no driver could be loaded.
wineconsole[834]: 005c:err:winediag:nodrv_CreateWindow L"The explorer process failed to start."
jkfloris
Level 12
Level 12
Posts: 3201
Joined: Thu Aug 14, 2014 10:10 am

Re: How to best run wine program as systemd service? Type=forking? PID of start.exe or wineserver?

Post by jkfloris »

Instead of using WINEDLLOVERRIDES, you can disable the programs in the registry.

Code: Select all

wine reg add "HKCU\Software\Wine\DllOverrides" /v explorer.exe
cj000
Level 2
Level 2
Posts: 12
Joined: Mon Apr 03, 2023 2:06 pm

Re: How to best run wine program as systemd service? Type=forking? PID of start.exe or wineserver?

Post by cj000 »

For now I will switch from "wineconsole" to "wine" to start mbserver.exe. The results in more output at systemctl status output:

Code: Select all

wine[1106]: XWall v3.59 (Win10x64 WINE)
wine[1106]: (c) copyright DataEnter GmbH 1991-2023
And I will not use ExecStop=wineserver -k.

Without the ExecStop and having Killmode=control-group (=default value), systemctl status output is without errors after a systemctl stop xwall.service:

Code: Select all

     Active: inactive (dead) since Fri 2023-04-14 20:35:30 CEST; 3s ago
   Duration: 34.514s
    Process: 1094 ExecStart=/usr/sbin/wine /home/user/mbserver.exe (code=killed, signal=TERM)
   Main PID: 1094 (code=killed, signal=TERM)
        CPU: 16.444s
Instead of:

Code: Select all

     Active: failed (Result: exit-code) since Fri 2023-04-14 20:33:33 CEST; 3s ago
   Duration: 55.865s
    Process: 1046 ExecStart=/usr/sbin/wine /home/user/mbserver.exe (code=exited, status=1/FAILURE)
    Process: 1071 ExecStop=/usr/sbin/wineserver -k (code=exited, status=0/SUCCESS)
   Main PID: 1046 (code=exited, status=1/FAILURE)
        CPU: 16.824s

Apr 14 20:32:38 host.domain.tld wine[1052]: 0034:err:win:get_desktop_window failed to start explorer c0000135
Apr 14 20:32:39 host.domain.tld wine[1058]:
Apr 14 20:32:39 host.domain.tld wine[1058]: XWall v3.59 (Win10x64 WINE)
Apr 14 20:32:39 host.domain.tld wine[1058]: (c) copyright DataEnter GmbH 1991-2023
Apr 14 20:32:39 host.domain.tld wine[1058]:
Apr 14 20:33:33 host.domain.tld systemd[1]: Stopping Xwall spam filter...
Apr 14 20:33:33 host.domain.tld systemd[1]: xwall.service: Main process exited, code=exited, status=1/FAILURE
Apr 14 20:33:33 host.domain.tld systemd[1]: xwall.service: Failed with result 'exit-code'.
Locked