Wie führe ich ein Bash-Skript mit einem Systemd-Dienst aus?

Wie führe ich ein Bash-Skript mit einem Systemd-Dienst aus?

Ich möchte eine grafische Anwendung über systemd starten. Ich habe das zuvor über udev versucht, aber die Anwendung wird nach Beendigung der Regel beendet. Es funktioniert, wenn ich die Anwendung direkt über systemd ausführe, aber nicht, wenn ich das Skript ausführe, das die Anwendung mit einigen Einschränkungen startet.

Meine Servicedatei sieht folgendermaßen aus:

[Unit]
Description=App starter

[Service]
Type=oneshot
RemainAfterExit=yes
Environment=DISPLAY=:0
Environment=HOME=/home/user/
ExecStart=/home/user/runApp

[Install]
WantedBy=multi-user.target

journalctl -xe gibt mir

Sep 25 13:50:00 host systemd[1]: Starting App starter...
Subject: A start job for unit app.service has begun execution
Defined-By: systemd
Support: https://forum.manjaro.org/c/support
 
A start job for unit xournalpp.service has begun execution.
 
The job identifier is 19196.
Sep 25 13:50:00 host runApp[313232]: App is not running
Sep 25 13:50:00 host systemd[1]: app.service: Main process exited, code=exited, status=1/FAILURE
Subject: Unit process exited
Defined-By: systemd
Support: https://forum.manjaro.org/c/support
 
An ExecStart= process belonging to unit xournalpp.service has exited.
 
The process' exit code is 'exited' and its exit status is 1.
Sep 25 13:50:00 host systemd[1]: app.service: Failed with result 'exit-code'.
Subject: Unit failed
Defined-By: systemd
Support: https://forum.manjaro.org/c/support
 
The unit xournalpp.service has entered the 'failed' state with result 'exit-code'.
Sep 25 13:50:00 host systemd[1]: Failed to start App starter.
Subject: A start job for unit app.service has failed
Defined-By: systemd
Support: https://forum.manjaro.org/c/support
 
A start job for unit app.service has finished with a failure.
 
The job identifier is 19196 and the job result is failed.

Hier ist das Skript. Liegt es vielleicht daran, dass es im Hintergrund ausgeführt wird &? Ich brauche es, weil ich das Skript wiederverwenden möchte.

#!/bin/bash

  if pgrep app
  then
      echo App is running
          wmctrl -x -a app
          wmctrl -x -r app -b "add,maximized_vert,maximized_horz"
          
  else
          echo App is not running
          /usr/bin/app & disown
  fi

Antwort1

Es handelt sich möglicherweise um ein Berechtigungsproblem.

/home/usererlaubt normalerweise keinem anderen Benutzer, darauf zuzugreifen, und Sicherheitssysteme wie Apparmor und Selinux untersagen normalerweise eine solche Verwendung.

Wenn Sie ein Skript in systemd verwenden möchten, fügen Sie es am besten ein /usr/local/bin.

verwandte Informationen