Linux/Ubuntu: Run window program with wine headless on server

PHOTO EMBED

Mon Aug 29 2022 16:38:46 GMT+0000 (UTC)

Saved by @marcopinero #bash

#!/bin/bash

#--- xvfb
sudo apt install -y xvfb

#-- add this into /etc/rc.local:

    #!/bin/sh -e
    Xvfb -ac :99 -screen 0 1024x768x16 &
    exit 0

#-- save & first run:
Xvfb -ac :99 -screen 0 1024x768x16 &

#--- wine
sudo dpkg --add-architecture i386

wget -O- -q https://download.opensuse.org/repositories/Emulators:/Wine:/Debian/xUbuntu_18.04/Release.key | sudo apt-key add -
echo "deb http://download.opensuse.org/repositories/Emulators:/Wine:/Debian/xUbuntu_18.04 ./" | sudo tee /etc/apt/sources.list.d/wine-obs.list

sudo apt update
sudo apt install --install-recommends winehq-stable winetricks

wine --version
wine --help

wineboot -u

winetricks allfonts

#-- install my app at /opt
sudo mkdir -p /opt/report/cache
sudo chmod -R 777 /opt/report
cp ReportService5.exe /opt/report
cd /opt/report

#-- and test it:
DISPLAY=:99 wine ReportService5.exe </dev/null &>/dev/null &

#-- create systemd service:

sudo nano /lib/systemd/system/report-service.service

[Unit]
Description=Reporting service

[Service]
Environment="DISPLAY=:99"
WorkingDirectory=/opt/report
ExecStart=/usr/bin/wine "/opt/report/ReportService5.exe" </dev/null &>/dev/null &
ExecStop=/opt/report/stop.sh
User=autana

[Install]
WantedBy=graphical.target

#-- save.

#-- create stop.sh

nano /opt/report/stop.sh

#!/bin/bash
kill $(pgrep ReportService5.exe)
kill -9 $(pgrep winedevice.exe)

#-- save.

sudo chmod +x /opt/report/stop.sh

#-- start service:
sudo systemctl enable report-service
sudo systemctl start report-service

DISPLAY=:99 import -window root -quality 90 /tmp/screenshot.jpg

content_copyCOPY

I have a windows program (.exe) that serves http api. I needed it to be running on my server (Ubuntu), but I didn't want to install virtualbox, VMWare or any other virtualization engine in my server. So I thought on wine and I got very good result.