Search code examples
linuxserviceraspberry-pisystemd

Program launches fine when exec'd manually but silently fails via systemd service


I have a C++ app that I can run manually but fails when being run by a systemd service with no error message. Why is this happening?

I can run the program myself just fine:

$ DISPLAY=:0 /home/pi/myApp/myApp &

But when I have this executed via systemd service it doesn't launch. The chain of execution is:

kiosk.service -> kiosk.sh -> myApp

Here is my systemd service (kiosk.service):

[Unit]
Description=Run app in kiosk mode
Wants=graphical.target
After=graphical.target

[Service]
ExecStartPre=/bin/sleep 20
Environment=DISPLAY=:0.0
Environment=XAUTHORITY=/home/pi/.Xauthority
Type=simple
ExecStart=/bin/bash /home/pi/kiosk.sh
Restart=on-abort
User=pi
Group=pi

[Install]
WantedBy=graphical.target

Here's kiosk.sh that runs my app:

#!/bin/bash

# This script has been adapted from: https://pimylifeup.com/raspberry-pi-kiosk/

# Disable screensaver / blank screen due to inactivity
xset s noblank
xset s off
xset -dpms

# Hide mouse cursor using the unclutter program
DISPLAY=:0 unclutter -idle 0.5 -root &

# Launch of app with our params
DISPLAY=:0 /home/pi/myApp/myApp &

$ systemctl status kiosk.service says:

○ kiosk.service - Launch app in kiosk mode
     Loaded: loaded (/lib/systemd/system/kiosk.service; enabled; preset: enabled)
     Active: inactive (dead) since Wed 2023-12-27 19:10:13 EST; 5min ago
   Duration: 48ms
   Main PID: 1483 (code=exited, status=0/SUCCESS)
        CPU: 54ms

Dec 27 19:09:53 raspberrypi systemd[1]: Starting kiosk.service - Launch app in kiosk mode...
Dec 27 19:10:13 raspberrypi systemd[1]: Started kiosk.service - Launch app in kiosk mode.
Dec 27 19:10:13 raspberrypi systemd[1]: kiosk.service: Deactivated successfully.

If I do $ sh /home/pi/kiosk.sh it also runs fine, so it seems nothing is wrong with my shell launcher script.

I'm thinking there's some environment variable context that changes when a program is executed from a systemd service? Any ideas?


Solution

  • There's no need to run the application in the background. All systemd services are running as daemons. So remove the & after MyApp.

    Your application apparently depends on libraries that aren't in the standard place, you need to set the LD_LIBRARY_PATH environment variable. So add

    Environment="LD_LIBRARY_PATH=/home/pi/myApp/libs:/usr/local/lib:/usr/lib:/lib"
    

    to the [Service] section.